package com.jwipc.finance.controller.rest;


import com.google.common.collect.Lists;
import com.jwipc.exception.DaoException;
import com.jwipc.finance.logic.PaymentPlanLogic;
import com.jwipc.finance.model.query.PaymentPlanQuery;
import com.jwipc.log.annotation.AuditLogInter;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

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

/**
 * <p>
 *     对账模块-付款计划 前端控制器
 * </p>
 *
 * @author 邓京术
 * @since 2025-07-22
 */
@RestController
@RequestMapping(MODULE_ROOT + "/payment/plan/rest")
@RequiredArgsConstructor
public class PaymentPlanRestController {
    private final PaymentPlanLogic paymentPlanLogic;

    /**
     * 查询付款计划单列表(采购或者财务)
     *
     * @param page 当前页码
     * @param rows 每页显示行数
     * @param query 查询条件
     * @return 结果
     */
    @AuditLogInter(value = "首页查询列表", module = "付款计划")
    @PostMapping("/list")
    public PageView list(Integer page, Integer rows, PaymentPlanQuery query){

        // 付款计划单数据状态 1:暂存 2:待采购主管确认 3:采购拒绝 4:待财务确认 5:财务驳回 6:已发布
        List<Integer> statusList = Lists.newArrayList();
        if(StringUtils.isNotEmpty(query.getAsStatus())) {
            String[] statusArray = StringUtils.split(query.getAsStatus(), ",");
            for (String status: statusArray) {
                statusList.add(Integer.parseInt(status));
            }
        } else {
            // 角色类型 1:采购角色 -> 1:暂存 2:待采购主管确认 3:采购拒绝 4:待财务确认 5:财务驳回 6:已发布
            if(null != query.getRoleType() && 1 == query.getRoleType()) {
                statusList.add(1);
                statusList.add(2);
                statusList.add(3);
                statusList.add(4);
                statusList.add(5);
                statusList.add(6);
            }

            // 角色类型 2:财务角色 -> 7:待财务确认 9:确认通过
            if(null != query.getRoleType() && 2 == query.getRoleType()) {
                statusList.add(4);
                statusList.add(6);
            }
        }
        query.setStatusList(statusList);
        return paymentPlanLogic.findByPage(query);
    }

    /**
     * 根据供应商和公司查询 供应商当月的付款计划汇总金额
     * @param supplierCode
     * @param companyCode
     * @return
     */
    @PostMapping("/findSupplierPaymentPlanSummary")
    public ResponseEntity findSupplierPaymentPlanSummary( String supplierCode,String companyCode, String paymentType) {
        if(Objects.isNull(companyCode) || Objects.isNull(supplierCode)) {
            throw new DaoException("参数不能为空!");
        }
        BigDecimal reStr = paymentPlanLogic.findSupplierPaymentPlanSummary(supplierCode, companyCode, 1, paymentType);
        return ResponseEntity.ok(reStr);
    }

//    /**
//     * 导出
//     * @param exportFiled 导出的列信息
//     * @param queryStr 查询条件
//     */
//    @PostMapping("/sup/export")
//    public void supExport(HttpServletResponse response, String exportFiled, String queryStr) throws IOException {
//        PaymentPlanQuery query = JSON.parseObject(queryStr, PaymentPlanQuery.class);
////        query.setIsSupplier(LoginUtil.isSupplier()?1:0);
//        if(LoginUtil.isSupplier()){
////            query.setSupplierCode(LoginUtil.getAccount());
//            // 1:暂存 2:待采购确认 3:采购拒绝 4:待推送OA审批 5:OA审批中 6:OA审批不通过 7:待财务确认 8:财务拒绝
//            List<Integer> statusList = Lists.newArrayList();
//            statusList.add(9);
////            query.setStatusList(statusList);
//        }else {
//            query.setUserId(LoginUtil.getUserId());
//            // 1:暂存 2:待采购确认 3:采购拒绝 4:待推送OA审批 5:OA审批中 6:OA审批不通过 7:待财务确认 8:财务拒绝
//            List<Integer> statusList = Lists.newArrayList();
//            statusList.add(1);
//            statusList.add(2);
//            statusList.add(3);
//            statusList.add(4);
//            statusList.add(5);
//            statusList.add(6);
//            statusList.add(7);
//            statusList.add(8);
//            statusList.add(9);
////            query.setStatusList(statusList);
//        }
//        // 查询数据列表，直接调用列表的查询，无需特殊改动
//        final List<PaymentPlanDTO> exportData = paymentPlanLogic.listAll(query);
//        //动态导出工具类，只需要修改导出excel名称
//        EasyExcelUtils.trendsModelWrite(response,"订单对账-" + DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN),"sheet", exportData, exportFiled);
//    }
//
//    /**
//     * 查询对账单列表(所有)
//     *
//     * @param query 查询条件
//     * @return 结果
//     */
//    @PostMapping("/all/list")
//    public PageView allList(PaymentPlanQuery query){
//        if(LoginUtil.isSupplier()){
////            query.setSupplierCode(LoginUtil.getAccount());
//            // 1:暂存 2:待采购确认 3:采购拒绝 4:待推送OA审批 5:OA审批中 6:OA审批不通过 7:待财务确认 8:财务拒绝
//            List<Integer> statusList = Lists.newArrayList();
//            statusList.add(9);
////            query.setStatusList(statusList);
//        }else {
//            query.setUserId(LoginUtil.getUserId());
//        }
//        return paymentPlanLogic.findByPage(query);
//    }

    /**
     * 付款计划单保存
     *
     * @param optType 操作类型；1-编辑；2-查看；3-采购主管确认；4-财务确认
     * @param headData 对账单抬头数据
     * @param statementItem 付款明细数据
     * @return 结果
     */
    @PostMapping("/saveOrUpdate")
    public ResponseEntity saveOrUpdate(Integer optType,String headData, String statementItem){
        paymentPlanLogic.saveOrUpdate(optType,headData,statementItem);
        return ResponseEntity.ok(true);
    }

    /**
     * 付款计划新增
     *
     * @param paymentPlanYear 对账单抬头数据
     * @param paymentPlanMonth 对账单明细数据
     * @return 结果
     */
    @PostMapping("/addNewPaymentPlan")
    public ResponseEntity addNewPaymentPlan(String paymentPlanYear,String paymentPlanMonth){
        paymentPlanLogic.addNewPaymentPlan(paymentPlanYear,paymentPlanMonth,true);
        return ResponseEntity.ok(true);
    }

    /**
     * 批量删除对账单
     *
     * @param ids 待删除的对账单ID
     *            type 1 删除 0 恢复
     * @return 结果
     */
    @PostMapping("/del")
    public ResponseEntity dataDel(String ids,Integer type) {
        paymentPlanLogic.dataDel(ids,type);
        return ResponseEntity.ok(true);
    }

    /**
     * 批量提交付款计划明细数据
     *
     * @param ids 待提交的付款计划明细ID
     * @return 结果
     */
    @PostMapping("/submit")
    public ResponseEntity submitConfirm(String ids){
        paymentPlanLogic.submitConfirm(ids);
        return ResponseEntity.ok(true);
    }

    /**
     * 批量提交对账单进行确认
     *
     * @param ids 待删除的对账单ID
     * @return 结果
     */
//    @PostMapping("/submit/confirm")
//    public ResponseEntity submitConfirm(String ids){
//        paymentPlanLogic.submitConfirm(ids);
//        return ResponseEntity.ok(true);
//    }


    /**
     * 付款计划审批操作
     * @param optType
     * @param headData
     * @return
     */
    @PostMapping("/approvalPayment")
    public ResponseEntity approveOperation(Integer optType,String headData){
        paymentPlanLogic.approveOperation(optType,headData);
        return ResponseEntity.ok(true);
    }

    /**
     * 修改付款备注
     */
//    @PostMapping("/savePaymentRemark")
//    public ResponseEntity savePaymentRemark(Long id,String paymentRemark){
//        PaymentPlanLogic.savePaymentRemark(id,paymentRemark);
//        return ResponseEntity.ok(true);
//    }


//    @PostMapping("/detailExport")
//    public void export(HttpServletResponse response, String exportFiled, String queryStr) throws IOException {
//
//        exportFiled = exportFiled.replace("createDate", "createDateStr").replace("contractDate","contractDateStr");
//        // 查询数据列表，直接调用列表的查询，无需特殊改动
//        final List<AccountSalesContractsDetailDTO> exportData = PaymentPlanLogic.findSalesContractListAll(
//                JSON.parseObject(queryStr, AccountSalesContractsDetailQuery.class));
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
//        for (AccountSalesContractsDetailDTO exportDatum : exportData) {
//            exportDatum.setDelFlag(exportDatum.getDelFlag().equals("0") ? "正常" : "删除");
//            exportDatum.setContractDateStr(exportDatum.getContractDate().format(formatter));
//            exportDatum.setCreateDateStr(exportDatum.getCreateDate().format(formatter));
//        }
//        //动态导出工具类，只需要修改导出excel名称
//        EasyExcelUtils.trendsModelWrite(response,
//                "购销合同明细数据-" + DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN), "sheet", exportData,
//                exportFiled);
//    }
}
