package com.ruoyi.hywz.controller.stamp.tax;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.hywz.common.enums.StampBusinessTypeEnum;
import com.ruoyi.hywz.common.enums.StampWarningTypeEnum;
import com.ruoyi.hywz.entity.*;
import com.ruoyi.hywz.entity.vo.stamp.tax.WarningDataVo;
import com.ruoyi.hywz.entity.vo.stamp.tax.WarningRule1Vo;
import com.ruoyi.hywz.entity.vo.stamp.tax.WarningRule2Vo;
import com.ruoyi.hywz.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 【印花税】预警表 前端控制器
 * </p>
 *
 * @author mybatisplus-generator
 * @date 2024-07-17 16:58:32
 */
@RestController
@RequestMapping("adsMatHyxtStampTaxWarningD")
@AllArgsConstructor
@Api(value = "adsMatHyxtStampTaxWarningD", tags = "印花税项目")
public class AdsMatHyxtStampTaxWarningDController {

    private final IAdsMatHyxtStampTaxWarningDService adsMatHyxtStampTaxWarningDService;
    private final IAdsMatHyxtStampTaxWarningRuleDService adsMatHyxtStampTaxWarningRuleDService;
    private final IAdsMatHyxtStampTaxContractDService adsMatHyxtStampTaxContractDService;
    private final IAdsMatHyxtStampTaxInvoiceDService adsMatHyxtStampTaxInvoiceDService;


    /**
     * 合同/发票预警
     */
    private final IAdsMatHyxtStampTaxContractInvoiceDService adsMatHyxtStampTaxContractInvoiceDService;

//    @ApiOperation(value = "3【预警表】分页查询", notes = "分页查询")
//    @Deprecated
//    @PostMapping("/queryPage")
//    public R queryPage(@RequestParam(defaultValue = "10") Long pageSize,
//                       @RequestParam(defaultValue = "1") Long pageNo,
//                       @RequestBody(required = false) AdsMatHyxtStampTaxWarningD param) {
//        return R.ok(adsMatHyxtStampTaxWarningDService.queryPage(pageSize, pageNo, param));
//    }

//    @ApiOperation(value = "3【预警表】消除预警", notes = "消除预警")
//    @PostMapping("/delWarning")
//    public R delWarning(@RequestBody AdsMatHyxtStampTaxWarningD param) {
//        AdsMatHyxtStampTaxWarningD warningD = adsMatHyxtStampTaxWarningDService.getById(param.getId());
//        if (ObjectUtils.isNotEmpty(warningD) && StringUtils.isNotBlank(warningD.getId())) {
//            warningD.setRemark(param.getRemark());
//            warningD.setUpdateTime(new Date());
//            warningD.setWarningType("");
//            warningD.setStatus(1);
//            adsMatHyxtStampTaxWarningDService.updateById(warningD);
//
//            adsMatHyxtStampTaxWarningDService.clearWarningByData(warningD);
//        }
//        return R.ok();
//    }

    @ApiOperation(value = "预警列表", notes = "预警列表")
    @PostMapping("/queryWarningList")
    public R queryWarningList(@RequestBody(required = false) AdsMatHyxtStampTaxWarningD param) {
        Integer sourceType = param.getSourceType();
        List<WarningDataVo> result = new ArrayList<>();
        WarningDataVo vo = new WarningDataVo();
        if (sourceType == 0) {
            AdsMatHyxtStampTaxContractD contractD = adsMatHyxtStampTaxContractDService.getById(param.getId());
            if (ObjectUtils.isNotEmpty(contractD)) {
                vo.setId(contractD.getId());
                vo.setSourceType(sourceType);
                vo.setWarningLevel(contractD.getWarningLevel());
                AdsMatHyxtStampTaxWarningRuleD ruleD = adsMatHyxtStampTaxWarningRuleDService.getByWarningType(contractD.getWarningType());
                if (null != ruleD) {
                    vo.setCheckGist(ruleD.getRule());
                    vo.setContent(ruleD.getDescription());
                }
                vo.setRemark(contractD.getWarningRemark());
                vo.setStatusDesc(contractD.getWarningStatusDesc());
                vo.setCreateTime(contractD.getWarningTime());
            }
            result.add(vo);
        } else {
            AdsMatHyxtStampTaxInvoiceD invoiceD = adsMatHyxtStampTaxInvoiceDService.getById(param.getId());
            vo.setId(invoiceD.getId());
            vo.setSourceType(sourceType);
            vo.setWarningLevel(invoiceD.getWarningLevel());
            AdsMatHyxtStampTaxWarningRuleD ruleD = adsMatHyxtStampTaxWarningRuleDService.getByWarningType(invoiceD.getWarningType());
            if (null != ruleD) {
                vo.setCheckGist(ruleD.getRule());
                vo.setContent(ruleD.getDescription());
            }
            vo.setRemark(invoiceD.getWarningRemark());
            vo.setStatusDesc(invoiceD.getWarningStatusDesc());
            vo.setCreateTime(invoiceD.getWarningTime());
            result.add(vo);
        }
        return R.ok(result);
    }


    @ApiOperation(value = "合同/发票信息", notes = "合同/发票信息")
    @PostMapping("/queryWarningListNew")
    public R queryWarningListNew(@RequestBody(required = false) AdsMatHyxtStampTaxContractInvoiceD param) {


        List<AdsMatHyxtStampTaxContractInvoiceD> contractInvoiceDS = adsMatHyxtStampTaxContractInvoiceDService.queryWarningListNew(param);


        return R.ok(contractInvoiceDS);
    }

    @ApiOperation(value = "3【预警表】风险规则清单", notes = "风险规则清单")
    @PostMapping("/warningList")
    public R warningList() {
        return R.ok(adsMatHyxtStampTaxWarningRuleDService.getList());
    }

    @ApiOperation(value = "3【预警表】风险规则清单", notes = "风险规则清单")
    @PostMapping("/warningRule")
    public R warningRule() {
        return R.ok(adsMatHyxtStampTaxWarningRuleDService.getList());
    }

    @ApiOperation(value = "3【预警表】风险规则清单2", notes = "风险规则清单2")
    @PostMapping("/warningRule1")
    public R warningRule1() {
        List<WarningRule1Vo> result = new ArrayList<>();
        result.add(new WarningRule1Vo("印花税应税合同税目选择不准确，少缴错缴印花税风险", "印花税"));
        result.add(new WarningRule1Vo("印花税计税依据不准确，少缴多缴印花税风险", "印花税"));
        result.add(new WarningRule1Vo("福利支出进项税额未足额转出风险", "增值税"));
        result.add(new WarningRule1Vo("劳务报酬个人所得税未足额代扣代缴风险", "个人所得税"));
        return R.ok(result);
    }

    @ApiOperation(value = "3【预警表】风险规则清单2", notes = "风险规则清单2")
    @PostMapping("/warningRule2")
    public R warningRule2() {
        List<WarningRule2Vo> result = new ArrayList<>();
        result.add(new WarningRule2Vo("强控","阻断","红色高亮，流程无法继续"));
        result.add(new WarningRule2Vo("弱控","预警","黄色高亮，需人工核实反馈"));
        return R.ok(result);
    }

    @ApiOperation(value = "导出")
    @PostMapping("/dataExport")
    public void dataExport(@RequestBody(required = false) AdsMatHyxtStampTaxWarningRuleD param, HttpServletResponse response) throws IOException {
        // id字段必传
        String id = param.getId();
        AdsMatHyxtStampTaxWarningRuleD ruleD = adsMatHyxtStampTaxWarningRuleDService.getById(id);
        if (null != ruleD) {
            Integer type = ruleD.getSourceType();
            if (type == 0) {
                // 导合同数据
                adsMatHyxtStampTaxContractDService.exportData(ruleD.getWarningType(), response);
            } else {
                // 导发票数据
                adsMatHyxtStampTaxInvoiceDService.exportData(ruleD.getWarningType(), response);
            }
        }
    }

    @ApiOperation(value = "", notes = "")
    @PostMapping("/warningType1")
    public R warningType1() {
        return R.ok(StampWarningTypeEnum.getList1());
    }

    @ApiOperation(value = "", notes = "")
    @PostMapping("/warningType2")
    public R warningType2() {
        return R.ok(StampWarningTypeEnum.getList2());
    }
}
