package com.jwipc.spe.controller;

import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import com.jwipc.exception.DaoException;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import com.jwipc.security.dto.CurrentUserDTO;
import com.jwipc.spe.dto.QmNoticeOrderDTO;
import com.jwipc.spe.logic.QmNoticeOrderLogic;
import com.jwipc.spe.logic.SupAppraiseDimensionLogic;
import com.jwipc.spe.model.query.QmNoticeOrderQuery;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
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 + "/supQualityManage/qualityHandle/rest/")
@RequiredArgsConstructor
public class SupQmNoticeOrderRestController {
    private final SupAppraiseDimensionLogic supAppraiseDimensionLogic;
    private final QmNoticeOrderLogic qmNoticeOrderLogic;
    //private final QmNoticeOrderMergeLogic qmNoticeOrderMergeLogic;
    /**
     * 列表
     *
     * @param query
     * @return
     */
    @RequestMapping("/list")
    public PageView list(QmNoticeOrderQuery query) {

//        final CurrentUserDTO user =  LoginUtil.getCurrentUser();
        query.setSupplierCode(LoginUtil.getSupplierCode());
        String buttonStatus = "2,3,4,5,7,9,10,12";
        List<String> statusList = Lists.newArrayList();
        statusList.addAll(Arrays.asList(StringUtils.split(buttonStatus, ",")));
        query.setStatusList(statusList);
        return qmNoticeOrderLogic.findByPage(query);
    }
    /**
     * 结案列表
     *
     * @param query
     * @return
     */
    @RequestMapping("/listEnd")
    public PageView listEnd(QmNoticeOrderQuery query) {

//        final CurrentUserDTO user =  LoginUtil.getCurrentUser();
        query.setSupplierCode(LoginUtil.getSupplierCode());
        String buttonStatus = "11";
        List<String> statusList = Lists.newArrayList();
        statusList.addAll(Arrays.asList(StringUtils.split(buttonStatus, ",")));
        query.setStatusList(statusList);
        return qmNoticeOrderLogic.findByPage(query);
    }

    /**
     * 整改明细列表数据
     *
     * @param query
     * @return
     */
//    @RequestMapping("/findRectifiedByPage")
//    public ResponseEntity<List<QmRectifiedMeasuresDTO>> findRectifiedByPage(Long id) {
//
//        return ResponseEntity.ok(qmNoticeOrderLogic.listAll(query));
//    }
//    public PageView findRectifiedByPage(Long id) throws Exception {
//        //String fatherId = qmNoticeOrderLogic.findFather(id, merge_flag);
//        QmRectifiedMeasuresDTO qmRectifiedMeasuresDTO = new QmRectifiedMeasuresDTO();
//        qmRectifiedMeasuresDTO.setNoticeOrderId(id);
//        pageView.setRows(rectifiedMeasuresService.findRectifiedByPage(rectifiedMeasuresFormMap));
//        return pageView;
//    }

        /**
         * 列表数据
         *
         * @param query
         * @return
         */
        @RequestMapping("/listAll")
        public ResponseEntity<List<QmNoticeOrderDTO>> listAll(QmNoticeOrderQuery query) {

        return ResponseEntity.ok(qmNoticeOrderLogic.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){

            qmNoticeOrderLogic.saveOrUpdateSup(param);

            return ResponseEntity.ok(true);


    }
    /**
     * 改善计划保存
     */
    @PostMapping("/improveSave")
    public ResponseEntity improveSave(@RequestBody QmNoticeOrderDTO param){
        try {
            qmNoticeOrderLogic.improve(param);

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

    /**
     * 删除整改措施
     */
    @ResponseBody
    @RequestMapping("removeMeasures")
    public int removeMeasures(String ids) {
        try {
            qmNoticeOrderLogic.removeMeasures(ids);
            return 0;
        } catch (Exception e) {
            e.printStackTrace();
            return 1;
        }
    }
    /**
     * 发布
     * @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);
    }
}
