package com.jwipc.spe.controller;

import com.google.common.collect.Lists;
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 + "/qualityManage/examineQualityManage/rest/")
@RequiredArgsConstructor
public class ExamineQualityManageRestController {
    private final SupAppraiseDimensionLogic supAppraiseDimensionLogic;
    private final QmNoticeOrderLogic qmNoticeOrderLogic;
    //private final QmNoticeOrderMergeLogic qmNoticeOrderMergeLogic;
    /**
     * 列表
     *
     * @param query
     * @return
     */
//    @RequestMapping("/findByPage")
//    public PageView list(QmNoticeOrderQuery query) {
//
//        final CurrentUserDTO user =  LoginUtil.getCurrentUser();
//        query.setSupplierCode(user.getLoginName());
//        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);
//    }

    /**
     * 质量整改列表
     */
    @ResponseBody
    @RequestMapping("findByPage")
    //public PageView findByPage(String page , String rows,String buttonStatus) throws Exception {
    public PageView findByPage(QmNoticeOrderQuery query) throws Exception {
//        NoticeOrderFormMap noticeOrderFormMap = getFormMap(NoticeOrderFormMap.class);
//        noticeOrderFormMap=toFormMap(noticeOrderFormMap, page, rows);
        List<String> statusList = Lists.newArrayList();
        if (StringUtils.isNotEmpty(query.getButtonStatus())){
            statusList.addAll(Arrays.asList(StringUtils.split(query.getButtonStatus(), ",")));
            query.setStatusList(statusList);
        } else {
//            List<ButtonVo> buttonVos = iButtomService.userButtons();
//            for (ButtonVo buttonVo : buttonVos){
//                if (buttonVo.getResKey().equals("deal_with")) {
//                    statusList.addAll(Arrays.asList(StringUtils.split(buttonVo.getDescription(), ",")));
//                }
//                buttonVo.getDescription();
//            }
            query.setStatusList(statusList);
        }
//        final Subject subject = SecurityUtils.getSubject();
//        final ShiroUser user = (ShiroUser) subject.getPrincipal();
        //noticeOrderFormMap.put("mqe_mce",user.getUserId());
        final CurrentUserDTO user = LoginUtil.getCurrentUser();
        query.setMqeMceName(user.getName());
        //来源（1:外包质量整改 2：一般质量整改）
        query.setSource(2);
       // pageView.setRows(noticeOrderService.findByNoticeOrderPage(noticeOrderFormMap));
        return qmNoticeOrderLogic.findByPage(query);
    }
        /**
         * 列表数据
         *
         * @param query
         * @return
         */
        @RequestMapping("/listAll")
        public ResponseEntity<List<QmNoticeOrderDTO>> listAll(QmNoticeOrderQuery query) {

        return ResponseEntity.ok(qmNoticeOrderLogic.listAll(query));
    }


    /**
     * SQE初审
     */
    @ResponseBody
    @RequestMapping("examination")
    public int examination(@RequestBody QmNoticeOrderDTO param) {
        try {
           int flag = qmNoticeOrderLogic.examination(param);
            return flag;
            //return ResponseEntity.ok(true);
        } catch (Exception e) {
            e.printStackTrace();
            return 1;
        }
    }

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

            return ResponseEntity.ok(true);
        } catch (Exception 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 noticeOrderCode 参数
     */
    @PostMapping("/remove")
    public ResponseEntity remove(String noticeOrderCode) {
        qmNoticeOrderLogic.remove(noticeOrderCode);
        return ResponseEntity.ok(true);
    }
}
