package com.jwipc.finance.controller.rest;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSON;
import com.google.common.collect.Lists;
import com.jwipc.finance.dto.*;
import com.jwipc.finance.logic.CostAccountStatementLogic;
import com.jwipc.finance.model.query.AccountStatementQuery;
import com.jwipc.finance.model.query.CostAccountStatementQuery;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import com.jwipc.sys.client.UserPurchaseGroupClient;
import com.jwipc.util.EasyExcelUtils;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

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

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

/**
 * <p>
 * 对账模块-报关单主表 前端控制器
 * </p>
 *
 * @author shenyang.ou
 * @since 2025-10-22
 */
@RestController
@RequestMapping(MODULE_ROOT + "/cost/account/statement/rest")
@RequiredArgsConstructor
public class CostAccountStatementRestController {
    private final CostAccountStatementLogic costAccountStatementLogic;
    private final UserPurchaseGroupClient userPurchaseGroupClient;

    @PostMapping("/list")
    public PageView list(CostAccountStatementQuery query){
        if (LoginUtil.isSupplier()){
            query.setSupplierCode(LoginUtil.getSupplierCode());
        }else {
            query.setUserId(LoginUtil.getUserId());
        }
        return costAccountStatementLogic.findByPage(query);
    }

    /**
     * 导出
     * @param exportFiled 导出的列信息
     * @param queryStr 查询条件
     */
    @PostMapping("/sup/export")
    public void supExport(HttpServletResponse response, String exportFiled, String queryStr) throws IOException {
        CostAccountStatementQuery query = JSON.parseObject(queryStr, CostAccountStatementQuery.class);
        if (LoginUtil.isSupplier()){
            query.setSupplierCode(LoginUtil.getSupplierCode());
        }else {
            query.setUserId(LoginUtil.getUserId());
        }
        // 查询数据列表，直接调用列表的查询，无需特殊改动
        final List<CostAccountStatementDTO> exportData = costAccountStatementLogic.listAll(query);
        //动态导出工具类，只需要修改导出excel名称
        EasyExcelUtils.trendsModelWrite(response,"费用对账-" + DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN),"sheet", exportData, exportFiled);
    }

    /**
     * 保存更新
     *
     * @param param 参数
     */
    @PostMapping("/saveOrUpdate")
    public ResponseEntity saveOrUpdate(@RequestBody CostAccountStatementParamDTO param) {
        costAccountStatementLogic.saveOrUpdate(param);
        return ResponseEntity.ok(true);
    }

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

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

    /**
     * 上传签章文件
     * @param signFile 文件id
     * @param id 对账单id
     */
    @RequestMapping("/saveSignFile")
    public ResponseEntity saveSignFile(Long signFile , Long id) {
//        if(files!=null&&files.length==0){
//            throw new DaoException("未上传盖章文件");
//        }
//        assert files != null;
        costAccountStatementLogic.saveSignFile(signFile,id);
        return ResponseEntity.ok(true);
    }

    /**
     * 一步发起签署
     * @param id 对账单id
     */
    @RequestMapping("/sendSign")
    public ResponseEntity sendSign(String id) {
        costAccountStatementLogic.sendSign(id);
        return ResponseEntity.ok(true);
    }

    /**
     * 采购撤销待财务确认状态的对账单
     *
     * @param asId 对账单ID
     * @param revokeReason 撤销原因
     * @return 结果
     */
    @PostMapping("/revoke/fin/confirm")
    public ResponseEntity revokeFinConfirm(Long asId, String revokeReason){
        costAccountStatementLogic.revokeFinConfirm(asId, revokeReason);
        return ResponseEntity.ok(true);
    }

    /**
     * 财务撤销已确认状态的对账单
     *
     * @param asId 对账单ID
     * @param revokeReason 撤销原因
     * @return 结果
     */
    @PostMapping("/revoke/fin/confirmed")
    public ResponseEntity revokeFinConfirmed(Long asId, String revokeReason){
        costAccountStatementLogic.revokeFinConfirmed(asId, revokeReason);
        return ResponseEntity.ok(true);
    }

    /**
     * 对账单审批操作
     * fin/account/statement/rest/approve
     *
     * @return 结果
     */
    @PostMapping("/purchase/approve")
    public ResponseEntity purchaseApproveOperation(@RequestBody CostAccountStatementApproveDTO params){

        costAccountStatementLogic.purchaseApproveOperation(params);

        return ResponseEntity.ok(true);
    }

    @PostMapping("/finance/approve")
    public ResponseEntity financeApproveOperation(@RequestBody CostAccountStatementApproveDTO params){

        costAccountStatementLogic.financeApproveOperation(params);

        return ResponseEntity.ok(true);
    }

    /**
     * 查询对账单或开票申请列表
     * @param query 查询条件
     * @return 结果
     */
    @PostMapping("/account/or/apply/list")
    public List<CostAccountStatementDTO> accountOrApplyList(CostAccountStatementQuery query){
        return costAccountStatementLogic.accountOrApplyList(query);
    }

}
