package com.jwipc.finance.controller.rest;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson2.JSON;
import com.google.common.collect.Lists;
import com.jwipc.finance.constant.SequenceConst;
import com.jwipc.finance.dto.DeductionDTO;
import com.jwipc.finance.dto.DeductionExportDTO;
import com.jwipc.finance.model.param.DeductionParam;
import com.jwipc.finance.model.query.DeductionExportQuery;
import com.jwipc.log.annotation.AuditLogInter;
import com.jwipc.page.PageView;
import com.jwipc.finance.logic.DeductionLogic;

import com.jwipc.finance.model.query.DeductionQuery;

import com.jwipc.security.LoginUtil;
import com.jwipc.security.dto.CurrentUserDTO;
import com.jwipc.sys.client.SequenceClient;
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 javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;

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


/**
 * @author Al
 * @create 2024-02-20 16:24
 * 对账管理-扣款管理
 */
@RestController
@RequestMapping(MODULE_ROOT + "/deduction/rest/")
@RequiredArgsConstructor
public class DedeuctionRestController {
    private final DeductionLogic deductionLogic;
    private final SequenceClient sequenceClient;
    /**
     * 列表
     *
     * @param query
     * @return
     */
    @RequestMapping("/list")
    public PageView list(DeductionQuery query) {
        //final CurrentUserDTO user =  LoginUtil.getCurrentUser();
        if(LoginUtil.isSupplier()) {
            query.setSupplierCode(LoginUtil.getSupplierCode());
            /**
             * 增加按状态查询条件， 2024-09-02号，邓京术
             */
            if (StringUtils.isEmpty(query.getStatus())) {
                String buttonStatus = "1,2,3,5,6";
                List<String> statusList = Lists.newArrayList();
                statusList.addAll(Arrays.asList(StringUtils.split(buttonStatus, ",")));
                query.setStatusList(statusList);
            }
        }
        return deductionLogic.findByPage(query);
    }
    @RequestMapping("/purchaseList")
    public PageView purchaseList(DeductionQuery query) {
        /**
         * 增加按状态查询条件， 2024-09-02号，邓京术
         */
        if (StringUtils.isEmpty(query.getStatus())) {
            String buttonStatus = "1,2,3,4,5,6";
            List<String> statusList = Lists.newArrayList();
            statusList.addAll(Arrays.asList(StringUtils.split(buttonStatus, ",")));
            query.setStatusList(statusList);
            }
        return deductionLogic.findByPage(query);
    }
    /**
     * 保存更新
     * @param param 参数
     */
    @PostMapping("/saveOrUpdateConfirm")
    public ResponseEntity saveOrUpdateConfirm(@RequestBody DeductionParam param){
        deductionLogic.saveOrUpdateConfirm(param);
        return ResponseEntity.ok(true);
    }
    @PostMapping("/saveOrUpdate")
    public ResponseEntity saveOrUpdate(@RequestBody DeductionParam param){
        String sequenceNo = "";
        if(Objects.isNull(param.getId())){
             sequenceNo = sequenceClient.getCodeSequenceNo("deduction_no", 3);

        }
        deductionLogic.saveOrUpdate(param,sequenceNo);
        return ResponseEntity.ok(true);
    }
    /**
     * 驳回
     * @param param 参数
     */
    @PostMapping("/refuse")
    public ResponseEntity refuse(@RequestBody DeductionParam param){
        String sequenceNo = "";
        if(Objects.isNull(param.getId())){
              sequenceNo = sequenceClient.getCodeSequenceNo("deduction_no", 3);

        }
        deductionLogic.refuse(param,sequenceNo);
        return ResponseEntity.ok(true);
    }
    /**
     * 提交更新
     * @param param 参数
     */
    @PostMapping("/submit")
    public ResponseEntity submit(@RequestBody DeductionParam param){
        String sequenceNo = "";
        if(Objects.isNull(param.getId())){
            sequenceNo = sequenceClient.getCodeSequenceNo("deduction_no", 3);

        }

        deductionLogic.submit(param,sequenceNo);
        return ResponseEntity.ok(true);
    }
    /**
     * 是否线下使用
     *
     * @return 页面
     */
    @PostMapping("/whether")
    public ResponseEntity whether(Long id, Integer whether){
        deductionLogic.whether(id,whether);
        //return ResponseEntity.ok("操作成功");
        return ResponseEntity.ok(true);
    }

    /**
     * 删除
     *
     * @param id 参数
     */
    @PostMapping("/remove")
    public ResponseEntity remove(String id) {
        deductionLogic.remove(id);
        return ResponseEntity.ok(true);
    }

    /**
     * 提交OA
     *
     * @param id 参数
     */
    @PostMapping("/submitOa")
    public ResponseEntity submitOa(String id) {
        deductionLogic.submitOa(id);
        return ResponseEntity.ok(true);
    }

    /**
     * 导出
     *
     */
    @GetMapping("deductionExportData")
    public void deductionExportData(HttpServletResponse response, DeductionExportQuery query) throws IOException {
        // 创建数据列表
        final List<DeductionExportDTO> forExport = deductionLogic.getDeductionForExport(query);

        // 设置响应头信息
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("扣款管理数据", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        // 使用EasyExcel导出Excel文件
        EasyExcel.write(response.getOutputStream(), DeductionExportDTO.class).sheet("Sheet1")
                .doWrite(forExport);
    }
    /**
     * 扣款列表导出
     * @param exportFiled 导出的列信息
     * @param queryStr 查询条件
     */
    @AuditLogInter(value = "导出数据", module = "扣款单创建")
    @PostMapping("/export")
    public void export(HttpServletResponse response, String exportFiled, String queryStr) throws IOException {
        // 查询数据列表，直接调用列表的查询，无需特殊改动
        final List<DeductionDTO> exportData = deductionLogic.listAll(JSON.parseObject(queryStr, DeductionQuery.class));
        //动态导出工具类，只需要修改导出excel名称
        EasyExcelUtils.trendsModelWrite(response,"扣款信息导出-" + DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN),"sheet", exportData, exportFiled);
    }
}
