package com.jwipc.spe.controller;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSON;
import com.google.common.collect.Lists;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import com.jwipc.security.dto.CurrentUserDTO;
import com.jwipc.spe.dto.QmAuditManageDTO;
import com.jwipc.spe.logic.QmAuditManageLogic;
import com.jwipc.spe.model.query.QmAuditManageQuery;
import com.jwipc.spe.service.IQmAuditManageService;
import com.jwipc.util.EasyExcelUtils;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import static com.jwipc.spe.constant.ModuleConst.MODULE_ROOT;

/**
 * @author Al
 * @create 2024-03-19 16:49
 * 稽核整改rest
 */
@RestController
@RequestMapping(MODULE_ROOT + "/qmAuditManage/rest/")
@RequiredArgsConstructor
public class QmAuditManageRestController {
    private final QmAuditManageLogic qmAuditManageLogic;
    private final IQmAuditManageService qmAuditManageService;
    /**
     * 列表
     *
     * @param query
     * @return
     */
    @RequestMapping("/list")
    public PageView list(QmAuditManageQuery query) {
        final CurrentUserDTO user =  LoginUtil.getCurrentUser();
        if(LoginUtil.isSupplier()){
        query.setSupplierCode(user.getLoginName());
            String buttonStatus = "0,1,2,4";
            List<String> statusList = Lists.newArrayList();
            statusList.addAll(Arrays.asList(StringUtils.split(buttonStatus, ",")));
            query.setStatusList(statusList);
        }
        return qmAuditManageLogic.findByPage(query);
    }


    /**
     * NCR保存附件
     *
     * @param id      品类id
     * @param fileIds 附件id 逗号分割
     * @return
     */
    @PostMapping("saveOnFile")
    public ResponseEntity saveOnFile(Long id, String fileIds) {
        qmAuditManageLogic.saveOnFile(id, fileIds);
        return ResponseEntity.ok().build();
    }
    /**
     * 稽核保存附件
     *
     * @param id      品类id
     * @param fileIds 附件id 逗号分割
     * @return
     */
    @PostMapping("saveOnFile1")
    public ResponseEntity saveOnFile1(Long id, String fileIds) {
        qmAuditManageLogic.saveOnFile1(id, fileIds);
        return ResponseEntity.ok().build();
    }
    /**
     * 保存更新
     * @param param 参数
     */
    @PostMapping("/saveOrUpdate")
    public ResponseEntity saveOrUpdate(@RequestBody QmAuditManageDTO param){
        qmAuditManageLogic.saveOrUpdate(param);
        return ResponseEntity.ok(true);
    }
    @PostMapping("/submit")
    public ResponseEntity submit(@RequestBody QmAuditManageDTO param){
        qmAuditManageLogic.submit(param);
        return ResponseEntity.ok(true);
    }
    @PostMapping("/saveOrUpdate1")
    public ResponseEntity saveOrUpdate1(@RequestBody QmAuditManageDTO param){
        qmAuditManageLogic.saveOrUpdate1(param);
        return ResponseEntity.ok(true);
    }
    @PostMapping("/saveOrUpdate2")
    public ResponseEntity saveOrUpdate2(@RequestBody QmAuditManageDTO param){
        qmAuditManageLogic.saveOrUpdate2(param);
        return ResponseEntity.ok(true);
    }

//    @PostMapping("/find")
//    public ResponseEntity find(String supplierCode, String companyCode){
//        qmAuditManageLogic.findPCode(supplierCode,companyCode);
//        return ResponseEntity.ok(true);
//    }
    @PostMapping("/find")
    public ResponseEntity find(String supplierId){
        return ResponseEntity.ok(qmAuditManageLogic.findPCode(supplierId));
    }

    /**
     * 稽核列表导出
     * @param exportFiled 导出的列信息
     * @param queryStr 查询条件
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, String exportFiled, String queryStr) throws IOException {
        // 查询数据列表，直接调用列表的查询，无需特殊改动
        final List<QmAuditManageDTO> exportData = qmAuditManageLogic.listAll(JSON.parseObject(queryStr, QmAuditManageQuery.class));
        //动态导出工具类，只需要修改导出excel名称
        EasyExcelUtils.trendsModelWrite(response,"稽核整改信息导出-" + DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN),"sheet", exportData, exportFiled);
    }

    /**
     * 稽核列表导入
     * @param file 导出的列信息
     */
    @PostMapping("import")
    public ResponseEntity importData(@RequestParam(value = "file") MultipartFile file) {
        qmAuditManageLogic.itemImportData(file);
        return ResponseEntity.ok().build();
    }
    /**
     * 删除
     *
     * @param id 参数
     */
    @PostMapping("/remove")
    public ResponseEntity remove(Long id) {
        qmAuditManageLogic.remove(id);
        return ResponseEntity.ok(true);
    }
}
