package com.jwipc.supplier.controller.pc;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSON;
import com.jwipc.page.PageView;
import com.jwipc.supplier.constant.ModuleConst;
import com.jwipc.supplier.convert.SupplierChangeVersionConvert;
import com.jwipc.supplier.convert.SupplierVersionConvert;
import com.jwipc.supplier.dto.ChangeExamineParamDTO;
import com.jwipc.supplier.dto.SupplierChangeSaveDTO;
import com.jwipc.supplier.dto.SupplierChangeVersionBaseDTO;
import com.jwipc.supplier.entity.SupplierChangeVersion;
import com.jwipc.supplier.logic.SupplierChangeLogic;
import com.jwipc.supplier.model.query.SupplierChangeQuery;
import com.jwipc.util.DateUtils;
import com.jwipc.util.EasyExcelUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

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

/**
 * <p> 供应商信息变更相关接口</p>
 *
 * @author ZhangJie
 * @version 1.0
 * @since JDK 1.8
 */
@RestController
@RequestMapping(ModuleConst.MODULE_ROOT + "/supplierChange/rest")
@RequiredArgsConstructor
public class SupplierChangeRestController {
    private final SupplierChangeLogic supplierChangeLogic;

    private final SupplierChangeVersionConvert supplierChangeVersionConvert;

    /**
     * 供应商变更列表数据
     *查询个人创建及自己邀请的供应商
     * @param query 查询参数
     * @return
     */
    @GetMapping("list")
    public PageView list(SupplierChangeQuery query) {
        return supplierChangeLogic.getList(query);
    }

    /**
     * 查询全部
     * @param query
     * @return
     */
    @GetMapping("listAll")
    public PageView listAll(SupplierChangeQuery query) {
        return supplierChangeLogic.getListAll(query);
    }

    @PostMapping("/export")
    public void export(HttpServletResponse response, String exportFiled, String queryStr) throws IOException {
        // 查询数据列表，直接调用列表的查询，无需特殊改动
        final List<SupplierChangeVersion> exportData = supplierChangeLogic.findExportListAll(JSON.parseObject(queryStr, SupplierChangeQuery.class));
        List<SupplierChangeVersionBaseDTO> supplierChangeVersionBaseDTOS = supplierChangeVersionConvert.entities2Dtos(exportData);
        exportFiled = exportFiled.replace("optType", "optTypeStr").replace("examineState","examineStateStr").replace("bpmState","bpmStateStr").replace("createTime","createTimeStr");
        supplierChangeVersionBaseDTOS.stream().forEach(version -> {
            switch (version.getOptType()) {
                case 1 :
                    version.setOptTypeStr("采购发起");
                    break;
                case 2 :
                    version.setOptTypeStr("供应商发起");
                    break;
                default:
                    version.setOptTypeStr("");
                    break;
            }
            switch (version.getExamineState()) {
                case 0 :
                    version.setExamineStateStr("待采购审批");
                    break;
                case 1 :
                    version.setExamineStateStr("通过");
                    break;
                case 2 :
                    version.setExamineStateStr("驳回");
                    break;
                default:
                    version.setExamineStateStr("");
                    break;
            }
            switch ( version.getBpmState()) {
                case 0 :
                    version.setBpmStateStr("待提交");
                    break;
                case 1 :
                    version.setBpmStateStr("审批中");
                    break;
                case 2 :
                    version.setBpmStateStr("通过");
                    break;
                case 3 :
                    version.setBpmStateStr("驳回");
                    break;
                default:
                    version.setBpmStateStr("");
                    break;
            }
            version.setCreateTimeStr(DateUtils.formatYYYYMMDDDash(version.getCreateTime()));
        });
        //动态导出工具类，只需要修改导出excel名称
        EasyExcelUtils.trendsModelWrite(response,
                "供应商变更报表-" + DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN), "sheet", supplierChangeVersionBaseDTOS,
                exportFiled);
    }
    /**
     * 供应商变更新增和更新接口
     *
     * @param supplierChangeSaveDTO 提交的数据
     * @return
     */
    @PostMapping("saveOrUpdate")
    public ResponseEntity saveOrUpdate(@RequestBody SupplierChangeSaveDTO supplierChangeSaveDTO) {
        supplierChangeLogic.saveOrUpdate(supplierChangeSaveDTO);
        return ResponseEntity.ok().build();
    }

    /**
     * 供应商信息变更基础字段变更列表
     *
     * @param query 查询参数
     * @return
     */
    @GetMapping("supplierVersionList")
    public PageView supplierVersionList(SupplierChangeQuery query) {
        return supplierChangeLogic.getSupplierVersionList(query);
    }

    /**
     * 供应商信息变更银行 变更列表
     *
     * @param query 查询参数
     * @return
     */
    @GetMapping("supplierBankVersionList")
    public PageView supplierBankVersionList(SupplierChangeQuery query) {
        return supplierChangeLogic.getSupplierBankVersionList(query);
    }

    /**
     * 供应商信息变更联系人变更列表
     *
     * @param query 查询参数
     * @return
     */
    @GetMapping("supplierContactsVersionList")
    public PageView supplierContactsVersionList(SupplierChangeQuery query) {
        return supplierChangeLogic.getSupplierContactsVersionList(query);
    }

    /**
     * 供应商信息变更品类变更列表
     *
     * @param query 查询参数
     * @return
     */
    @GetMapping("supplierPurchaseCategoryVersionList")
    public PageView supplierPurchaseCategoryVersionList(SupplierChangeQuery query) {
        return supplierChangeLogic.getSupplierPurchaseCategoryVersionList(query);
    }

    /**
     * 供应商信息变更资质数据
     *
     * @param query 查询参数
     * @return
     */
    @GetMapping("supplierQualificationVersionList")
    public PageView supplierQualificationVersionList(SupplierChangeQuery query) {
        return supplierChangeLogic.getSupplierQualificationVersionList(query);
    }

    /**
     * 供应商信息变更附件列表
     *
     * @param query 查询参数
     * @return
     */
    @GetMapping("attachmentList")
    public PageView attachmentList(SupplierChangeQuery query) {
        return supplierChangeLogic.getAttachmentList(query);
    }

    /**
     * 供应商信息变更 商务审批
     *
     * @param id     供应商信息变更主键
     * @param state  1:通过;2:驳回
     * @param remark 审批意见
     * @return
     */
    @PostMapping("examine")
    public ResponseEntity examine(@RequestBody ChangeExamineParamDTO paramDTO) {
        supplierChangeLogic.examine(paramDTO);
        return ResponseEntity.ok().build();
    }

    /**
     * 模拟OA审批
     *
     * @param id    供应商信息变更主键
     * @param state 2:通过;3:驳回
     * @return
     */
    @PostMapping("oaExamine")
    public ResponseEntity oaExamine(Long id, Integer state) {
        supplierChangeLogic.oaExamine(id, state);
        return ResponseEntity.ok().build();
    }

    //==================
    /**
     * 供应商信息变更银行 变更列表
     *
     * @param query 查询参数
     * @return
     */
    @GetMapping("supplierBankVersionWholeList")
    public PageView supplierBankVersionWholeList(SupplierChangeQuery query) {
        return supplierChangeLogic.getSupplierBankVersionWholeList(query);
    }

    /**
     * 供应商信息变更联系人变更列表
     *
     * @param query 查询参数
     * @return
     */
    @GetMapping("supplierContactsVersionWholeList")
    public PageView supplierContactsVersionWholeList(SupplierChangeQuery query) {
        return supplierChangeLogic.getSupplierContactsVersionWholeList(query);
    }

    /**
     * 供应商信息变更品类变更列表
     *
     * @param query 查询参数
     * @return
     */
    @GetMapping("supplierPurchaseCategoryVersionWholeList")
    public PageView supplierPurchaseCategoryVersionWholeList(SupplierChangeQuery query) {
        return supplierChangeLogic.getSupplierPurchaseCategoryVersionWholeList(query);
    }

    /**
     * 供应商信息变更资质数据
     *
     * @param query 查询参数
     * @return
     */
    @GetMapping("supplierQualificationVersionWholeList")
    public PageView supplierQualificationVersionWholeList(SupplierChangeQuery query) {
        return supplierChangeLogic.getSupplierQualificationVersionWholeList(query);
    }

    /**
     * 供应商信息变更附件列表
     *
     * @param query 查询参数
     * @return
     */
    @GetMapping("attachmentWholeList")
    public PageView attachmentWholeList(SupplierChangeQuery query) {
        return supplierChangeLogic.getAttachmentWholeList(query);
    }


    /**
     * 删除明细
     *
     * @param id    供应商信息变更主键
     * @return
     */
    @PostMapping("remove")
    public ResponseEntity remove(Long id) {
        supplierChangeLogic.remove(id);
        return ResponseEntity.ok().build();
    }
}
