package com.sifang.modules.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.CrmUtils;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseExceptionController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

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

//质量整改
@Controller
@RequestMapping(value = "${adminPath}/entryQuality/qualityRectification")
public class CrmQualityRectificationController extends BaseExceptionController {

    @Autowired
    private CrmQualityRectificationService qualityRectificationService;

    @Autowired
    private CrmEntryQualityAcceptanceService qualityAcceptanceService;

    @Autowired
    private TraderService traderService;

    @Autowired
    private CrmEntryQualityAcceptanceResultService resultService;

    //质量整改列表页面
    @RequestMapping(value = "qualityRectificationPage")
    public String entryQualityPage(Model model){
        //代理商列表
        List<Record> traderList = traderService.getTopTrader();
        model.addAttribute("traderList", traderList);
        User user= UserUtils.getUser();
        model.addAttribute("user",user);
        return "modules/quality/qualityRectificationPage";
    }

    //列表数据
    @ResponseBody
    @RequestMapping(value = "getQualityRectificationList")
    public Object getQualityRectificationList(HttpServletRequest request){
        Map<String, String> map = getParams(request);
        User user = UserUtils.getUser();
        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.getId(), CrmUtils.getUserXM(user),user.getUserType());
        qualityRectificationService.getQualityRectificationList(map,page,operator);
        return page.renderJson();
    }

    //派工页面
    @RequestMapping(value = "qualityRectificationDispatchPage")
    public String entryQualityDispatchPage(String id, String mainId, String traderId, Model model){
        Record detailRecord = qualityAcceptanceService.getOrderDetail(mainId);
        model.addAttribute("detailRecord",detailRecord);
        model.addAttribute("id",id);
        model.addAttribute("traderId",traderId);
        return "modules/quality/qualityRectificationDispatchPage";
    }

    //获取派工的列表数据
    @ResponseBody
    @RequestMapping(value = "getDispatchEmpList")
    public Object getDispatchEmpList(HttpServletRequest request){
        Map<String, String> map = getParams(request);
        LayuiPage page = new LayuiPage(request);
        User user = UserUtils.getUser();
        Operator operator = Operator.of(user.getId(), CrmUtils.getUserXM(user),user.getUserType());
        List<Record> list = qualityRectificationService.getDispatchEmpList(map,operator);
        page.setData(list);
        return  page.renderJson();
    }

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

    /**
     * 查看
     */
    @RequestMapping(value = "qualityRectificationDetail")
    public String qualityRectificationDetail(String id,Model model){
        CrmEntryQualityAcceptanceDetail acceptanceDetail = qualityAcceptanceService.canNotAcceptanceDetail(id);
        Record detailRecord = qualityAcceptanceService.getOrderDetail(acceptanceDetail.getMainId());
        model.addAttribute("detailRecord",detailRecord);
        model.addAttribute("acceptanceDetail",acceptanceDetail);
        //获取合格项
        List<ResultRes> resultRight = qualityRectificationService.getAcceptResultByDetailId(id,"0");
        model.addAttribute("resultRight",resultRight);
        //获取不合格项
        List<ResultRes> resultWrong = resultService.getAcceptResultByDetailId(id,"1");
        Record count=resultService.getResultCount(id);
        model.addAttribute("resultWrong",resultWrong);
        model.addAttribute("count",count);
        return "modules/quality/qualityRectificationDetail";
    }

    /**
     * 电站整改
     */
    @RequestMapping(value = "qualityRectificationChanging")
    public String waitChangeQualityDetail(String id,Model model){
        CrmEntryQualityAcceptanceDetail acceptanceDetail = qualityAcceptanceService.canNotAcceptanceDetail(id);
        Record detailRecord = qualityAcceptanceService.getOrderDetail(acceptanceDetail.getMainId());
        model.addAttribute("detailRecord",detailRecord);
        model.addAttribute("acceptanceDetail",acceptanceDetail);
        //获取合格项
        List<ResultRes> resultRight = resultService.getAcceptResultByDetailId(id,"0");
        model.addAttribute("resultRight",resultRight);
        //获取不合格项
        List<ResultRes> resultWrong = resultService.getAcceptResultByDetailId(id,"1");
        Record count=resultService.getResultCount(id);
        model.addAttribute("count",count);
        model.addAttribute("resultWrong",resultWrong);
        model.addAttribute("detailId",id);
        return "modules/quality/qualityRectificationChanging";
    }

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

    /**
     * 验收
     */
    @RequestMapping(value = "qualityRectificationAccept")
    public String qualityRectificationAccept(String id,Model model){
        CrmEntryQualityAcceptanceDetail acceptanceDetail = qualityAcceptanceService.canNotAcceptanceDetail(id);
        Record detailRecord = qualityAcceptanceService.getOrderDetail(acceptanceDetail.getMainId());
        model.addAttribute("detailRecord",detailRecord);
        model.addAttribute("acceptanceDetail",acceptanceDetail);
        //获取合格项
        List<ResultRes> resultRight = resultService.getAcceptResultByDetailId(id,"0");
        model.addAttribute("resultRight",resultRight);
        //获取不合格项
        List<ResultRes> resultWrong = resultService.getAcceptResultByDetailId(id,"1");
        Record count=resultService.getResultCount(id);
        model.addAttribute("count",count);
        model.addAttribute("resultWrong",resultWrong);
        model.addAttribute("id",id);
        return "modules/quality/qualityRectificationAccept";
    }

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

}