package com.winsun.controller;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.winsun.auth.core.annotion.Permission;
import com.winsun.auth.core.base.controller.BaseController;
import com.winsun.auth.core.common.model.ResponseData;
import com.winsun.auth.core.shiro.ShiroUser;
import com.winsun.bean.ExportExcel;
import com.winsun.mapper.ExportExcelMapper;
import com.winsun.service.ExportExcelService;
import com.winsun.service.impl.ExportExcelServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.List;

/**
 * 智能平台账号实名管理
 *
 * @Author: chancy
 * @Date:
 */
@Slf4j
@RestController
@RequestMapping("/exportExcel")
public class ExportExcelController extends BaseController {

    private static ExportExcelMapper exportExcelMapper;
    private static ExportExcelService exportExcelService;

    @Autowired
    public ExportExcelController(ExportExcelMapper exportExcelMapper, ExportExcelServiceImpl exportExcelService) {
        ExportExcelController.exportExcelMapper = exportExcelMapper;
        ExportExcelController.exportExcelService = exportExcelService;
    }


    @Permission(menuname = "订单excel文件下载", value = "uploadExcel", method = RequestMethod.POST)
    public void uploadExcel(String exportId) {
        ExportExcel exportExcel = exportExcelMapper.selectById(exportId);
        HttpServletResponse response = getHttpServletResponse();
        exportExcelService.uploadExcel(exportExcel, response);
    }


    @Permission(menuname = "查询下载列表", value = "listExportExcel", method = RequestMethod.POST)
    public ResponseData<Page<ExportExcel>> listExportExcel(@RequestParam(value = "fileName", required = false) String fileName, @RequestParam(name = "pageNo") int pageIndex, @RequestParam(name = "pageSize") int pageSize) {
        ShiroUser user = getShiroUser();
        Wrapper<ExportExcel> wrapper = new EntityWrapper();
        if (!user.getRoleNames().stream().anyMatch(roleName -> StringUtils.equalsAny(roleName, "超级管理员", "快递公司"))) {
            wrapper.like("create_user", user.getId().toString(), SqlLike.DEFAULT);
        }
        wrapper.like(StringUtils.isNotBlank(fileName), "file_name", fileName, SqlLike.DEFAULT);
        Page<ExportExcel> page = new Page<>(pageIndex, pageSize);
        List<ExportExcel> exportExcels = exportExcelMapper.selectPage(page, wrapper);
        page.setRecords(exportExcels);
        return ResponseData.success(page, "查询成功！");
    }

    @Permission(menuname = "删除文件", value = "deleteExcel", method = RequestMethod.POST)
    public ResponseData<String> deleteExcel(String exportId) {
        ExportExcel exportExcel = exportExcelMapper.selectById(exportId);
        File file = new File(ExportExcelServiceImpl.EXPORT + exportExcel.getExportUrl());
        Boolean flag = false;
        //判断文件是否存在
        if (file.exists() == true) {
            flag = file.delete();
            if (flag) {
                exportExcelMapper.deleteById(Integer.parseInt(exportId));
                log.info("成功删除" + file.getName());
                return ResponseData.success("成功删除" + file.getName() + "！");
            } else {
                log.error("删除失败" + file.getName());
                return ResponseData.error("删除失败！");
            }
        } else {
            log.info(file.getName() + "不存在，终止操作");
        }
        return ResponseData.error("删除失败！");
    }
}
