package com.jwipc.spe.controller;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jwipc.exception.DaoException;
import com.jwipc.page.PageUtil;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import com.jwipc.security.dto.CurrentUserDTO;
import com.jwipc.spe.convert.QmNoticeOrderConvert;
import com.jwipc.spe.dto.QmNoticeOrderDTO;
import com.jwipc.spe.dto.SupAppraiseDimensionDTO;
import com.jwipc.spe.entity.QmNoticeOrder;
import com.jwipc.spe.logic.QmNoticeOrderLogic;
import com.jwipc.spe.logic.SupAppraiseDimensionLogic;
import com.jwipc.spe.model.po.QmNoticeOrderPO;
import com.jwipc.spe.model.query.QmNoticeOrderQuery;
import com.jwipc.spe.model.query.SupAppraiseDimensionQuery;
import com.jwipc.spe.service.IQmNoticeOrderService;
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;

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

/**
 * @author Al
 * @create 2024-02-28 16:01
 * 考核指标
 */
@RestController
@RequestMapping(MODULE_ROOT + "/qualityManage/launchedQualityManage/rest/")
@RequiredArgsConstructor
public class QmNoticeOrderRestController {
    private final SupAppraiseDimensionLogic supAppraiseDimensionLogic;
    private final QmNoticeOrderLogic qmNoticeOrderLogic;
    private final IQmNoticeOrderService qmNoticeOrderService;
    private final QmNoticeOrderConvert qmNoticeOrderConvert;

    /**
     * 列表
     *
     * @param query
     * @return
     */
    @RequestMapping("/list")
    public PageView list(QmNoticeOrderQuery query) {
        return qmNoticeOrderLogic.findByPage(query);
    }

    /**
     * 跟踪状态改变
     *
     * @return 页面
     */
    @PostMapping("/following")
    public ResponseEntity whether(Long id, Integer followingStatus) {
        qmNoticeOrderLogic.followingChange(id, followingStatus);
        //return ResponseEntity.ok("操作成功");
        return ResponseEntity.ok(true);
    }

    /**
     * 保存更新
     *
     * @param param 参数
     */
    @PostMapping("/examine")
    public ResponseEntity examine(@RequestBody QmNoticeOrderDTO param) {
        try {
            qmNoticeOrderLogic.examine(param);

            return ResponseEntity.ok(true);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 保存更新
     *
     * @param param 参数
     */
    @PostMapping("/examine1")
    public ResponseEntity examine1(@RequestBody QmNoticeOrderDTO param) {
        try {
            qmNoticeOrderLogic.examine1(param);

            return ResponseEntity.ok(true);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 列表数据
     *
     * @param query
     * @return
     */
    @RequestMapping("/listAll")
    public ResponseEntity<List<SupAppraiseDimensionDTO>> listAll(SupAppraiseDimensionQuery query) {
        query.setDimensionStatus(1);
        return ResponseEntity.ok(supAppraiseDimensionLogic.listAll(query));
    }

    /**
     * 质量整改归并列表
     */
//    @ResponseBody
//    @RequestMapping("findMergeByPage")
//    public PageView findMergeByPage(String page , String rows,Long id,int mergeFlag) throws Exception {
//        QmNoticeOrder qmNoticeOrder = getFormMap(QmNoticeOrder.class);
//        qmNoticeOrder=toFormMap(qmNoticeOrder, page, rows);
//        qmNoticeOrder.setId(id);
//        //如果为父级
//        if (mergeFlag == 1){
//            pageView.setRows(qmNoticeOrderLogic.findFatherMergeByPage(qmNoticeOrder));
//            //如果为子级
//        }else if (mergeFlag == 2){
//            pageView.setRows(qmNoticeOrderLogic.findSonMergeByPage(id));
//        }
//        return pageView;
//    }

    /**
     * 保存更新
     *
     * @param param 参数
     */
    @PostMapping("/saveOrUpdate")
    public ResponseEntity saveOrUpdate(@RequestBody QmNoticeOrderDTO param) {
        try {
            qmNoticeOrderLogic.saveOrUpdate(param);

            return ResponseEntity.ok(true);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }


    }

    /**
     * 发布
     *
     * @param id 参数
     */
    @PostMapping("/release")
    public ResponseEntity qmRelease(Long id) {
        try {
            qmNoticeOrderLogic.release(id);

            return ResponseEntity.ok(true);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }


    }

    /**
     * 启用或停用维度指标数据
     *
     * @param ids    数据ID
     * @param status 1:启用 2:停用
     * @return 结果
     */
    @PostMapping("/invalidOrEffective")
    public ResponseEntity invalidOrEffectiveTarget(String ids, Integer status) {
        final CurrentUserDTO user = LoginUtil.getCurrentUser();
        final boolean b = supAppraiseDimensionLogic.invalidOrEffectiveTarget(ids, status, user);
        if (!b) {
            return ResponseEntity.badRequest().body("操作失败");
        }
        return ResponseEntity.ok(StrUtil.EMPTY);
    }

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

    /**
     * 质量整改列表导出
     *
     * @param exportFiled 导出的列信息
     * @param queryStr    查询条件
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, String exportFiled, String queryStr) throws IOException {
        QmNoticeOrderQuery query = JSON.parseObject(queryStr, QmNoticeOrderQuery.class);
        final Page<QmNoticeOrder> dbPage = PageUtil.toPage(query);
        List<QmNoticeOrderPO> qmNoticeOrderPOS = qmNoticeOrderService.findPageByQuery(dbPage, query);
        if (qmNoticeOrderPOS.size() > 10000) {
            throw new DaoException("超过最大导出条数");
        }
        List<QmNoticeOrderDTO> exportData = qmNoticeOrderConvert.po2PageListDTO(qmNoticeOrderPOS);

        exportFiled = exportFiled.replace("status", "statusStr").replace("followingStatus","followingStatusStr").replace("createTime","createTimeStr").replace("releaseTime","releaseTimeStr");
        exportData.stream().forEach(item -> {
            switch (item.getStatus()) {
                case 0 :
                    item.setStatusStr("新建");
                    break;
                case 1 :
                    item.setStatusStr("发布");
                    break;
                case 2 :
                    item.setStatusStr("供方待确认");
                    break;
                case 3 :
                    item.setStatusStr("待复核");
                    break;
                case 4 :
                    item.setStatusStr("待SQE初审");
                    break;
                case 5 :
                    item.setStatusStr("初审不同意");
                    break;
                case 6 :
                    item.setStatusStr("待主管审批");
                    break;
                case 7 :
                    item.setStatusStr("主管审批不同意");
                    break;
                case 8 :
                    item.setStatusStr("待经理审批");
                    break;
                case 9 :
                    item.setStatusStr("经理审批不同意");
                    break;
                case 10 :
                    item.setStatusStr("待跟踪");
                    break;
                case 11 :
                    item.setStatusStr("已结案");
                    break;
                default:
                    item.setStatusStr("");
                    break;
            }
            switch (item.getFollowingStatus()) {
                case 1 :
                    item.setFollowingStatusStr("跟踪");
                    break;
                case 2 :
                    item.setFollowingStatusStr("关闭");
                    break;
                default:
                    item.setStatusStr("");
                    break;
            }
            item.setCreateTimeStr(DateUtils.formatYYYYMMDDDash(item.getCreateTime()));
            item.setReleaseTimeStr(item.getReleaseTime() == null?"":DateUtils.formatYYYYMMDDDash(item.getReleaseTime()));
        });

        //动态导出工具类，只需要修改导出excel名称、
        EasyExcelUtils.trendsModelWrite(response,"质量整改列表信息导出-" + DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN),"sheet", exportData, exportFiled);
    }
}