package com.sifang.modules.smallprogram.quality.controller;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.quality.entity.CrmEntryQualityAcceptanceDetail;
import com.sifang.modules.quality.service.CrmEntryQualityAcceptanceResultService;
import com.sifang.modules.quality.service.CrmEntryQualityAcceptanceService;
import com.sifang.modules.quality.service.CrmQualityRectificationService;
import com.sifang.modules.quality.vo.DispatchEmpVo;
import com.sifang.modules.quality.vo.QualityChangingDetailVo;
import com.sifang.modules.quality.vo.ResultRes;
import com.sifang.modules.sunshine.SystemSetting.service.TraderService;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.entity.Operator;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseExceptionController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


//质检整改
@RestController
@RequestMapping(value = "${adminPath}/main/quality/rectification")
public class SmallQualityRectificationController extends BaseExceptionController {

    @Autowired
    private CrmQualityRectificationService qualityRectificationService;

    @Autowired
    private CrmEntryQualityAcceptanceService qualityAcceptanceService;

    @Autowired
    private TraderService traderService;

    @Autowired
    private CrmEntryQualityAcceptanceResultService resultService;


    //列表数据
    @RequestMapping(value = "getQualityRectificationList")
    public Object getQualityRectificationList(HttpServletRequest request){
        Map<String, String> map = getParams(request);
        String userId = request.getParameter("userId");
        User user = UserUtils.getUser(userId);
        map.put("userId",user.getId());
        if(User.USER_TYPE_WXG.equals(user.getUserType())){//是平台的员工
            map.put("sign","examine");
        }else if(User.USER_TYPE_ADVISOR.equals(user.getUserType())){//代理商员工
            map.put("sign","work");
        }
        LayuiPage page = new LayuiPage(request);
        Operator operator = Operator.of(user);
        qualityRectificationService.getQualityRectificationList(map,page,operator);
        page.setCode("200");
        return page.renderJson();
    }

    //获取派工的列表数据
    @RequestMapping(value = "getDispatchEmpList")
    public Result getDispatchEmpList(HttpServletRequest request){
        String userId = request.getParameter("userId");
        User user = UserUtils.getUser(userId);
        Map<String, String> map = getParams(request);
        Operator operator = Operator.of(user);
        List<Record> list = qualityRectificationService.getDispatchEmpList(map,operator);
        return Result.success(list);
    }

    /**
     * 派工
     */
    @ResponseBody
    @RequestMapping(value = "saveDispatchEmp")
    public Result saveDispatchEmp(@RequestBody DispatchEmpVo req) {
        qualityRectificationService.saveDispatchEmp(req);
        return Result.ok();
    }

    //详情
    @RequestMapping(value = "qualityRectificationDetail")
    public Result qualityDetailData(String id){
        CrmEntryQualityAcceptanceDetail acceptanceDetail = qualityAcceptanceService.canNotAcceptanceDetail(id);
        Record detailRecord = qualityAcceptanceService.getOrderDetail(acceptanceDetail.getMainId());
        //获取合格项
        List<ResultRes> resultRight = qualityRectificationService.getAcceptResultByDetailId(id,"0");
        //获取不合格项
        List<ResultRes> resultWrong = qualityRectificationService.getAcceptResultByDetailId(id,"1");
        Record count=resultService.getResultCount(id);
        Map<String,Object> resultMap=new HashMap<>();
        resultMap.put("detailRecord",detailRecord);
        resultMap.put("acceptanceDetail",acceptanceDetail);
        resultMap.put("resultRight",resultRight);
        resultMap.put("resultWrong",resultWrong);
        resultMap.put("detailId",id);
        resultMap.put("count",count);
        return Result.success(resultMap);
    }

    /**
     * 电站整改操作
     */
    @ResponseBody
    @RequestMapping(value = "saveChangingQuality")
    public Result saveChangingQuality(@RequestBody QualityChangingDetailVo change) {
        qualityRectificationService.saveChangingQuality(change);
        return Result.ok();
    }

    /**
     * 验收操作
     */
    @ResponseBody
    @RequestMapping(value = "saveAcceptQuality")
    public Result saveAcceptQuality(String id, String changeAcceptRemarks,String changeStatus) {
        qualityRectificationService.saveAcceptQuality(id,changeAcceptRemarks,changeStatus);
        return Result.ok();
    }
}