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.vo.DispatchEmpVo;
import com.sifang.modules.quality.vo.QualityAcceptVo;
import com.sifang.modules.quality.vo.ResultRes;
import com.sifang.modules.sunshine.SystemSetting.dao.CrmPlatformEmpDao;
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.common.utils.DateUtils;
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.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 javax.validation.Valid;
import java.util.List;
import java.util.Map;

//质量验收
@Controller
@RequestMapping(value = "${adminPath}/entryQuality/qualityManagement")
public class CrmEntryQualityAcceptanceController extends BaseExceptionController {

    @Autowired
    private CrmEntryQualityAcceptanceService qualityAcceptanceService;

    @Autowired
    private CrmPlatformEmpDao platformEmpDao;

    @Autowired
    private TraderService traderService;

    @Autowired
    private CrmEntryQualityAcceptanceResultService resultService;

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

    //列表数据
    @ResponseBody
    @RequestMapping(value = "entryQualityData")
    public Object entryQualityData(HttpServletRequest request){
        Map<String, String> map = getParams(request);
        LayuiPage page = new LayuiPage(request);
        qualityAcceptanceService.entryQualityData(map,page);
        return page.renderJson();
    }

    //派工页面
    @RequestMapping(value = "entryQualityDispatchPage")
    public String entryQualityDispatchPage(String id, Model model){
        model.addAttribute("id",id);
        return "modules/quality/entryQualityDispatchPage";
    }

    //获取派工的列表数据
    @ResponseBody
    @RequestMapping(value = "getDispatchEmpData")
    public Object getDispatchEmpData(HttpServletRequest request){
        Map<String, String> map = getParams(request);
        LayuiPage page = new LayuiPage(request);
        List<Record> list=qualityAcceptanceService.getDispatchEmpData(map);
        page.setData(list);
        return  page.renderJson();
    }


    /**
     * 派工
     */
    @ResponseBody
    @RequestMapping(value = "saveDispatchEmp")
    public Result saveDispatchEmp(@RequestBody DispatchEmpVo req) {
        User user= UserUtils.getUser();
        Operator operator=Operator.of(user);
        qualityAcceptanceService.saveDispatchEmp(req,operator);
        return Result.ok();
    }

    //详情页面
    @RequestMapping(value = "detailPage")
    public String detailPage(String id,Model model){
        Record detailRecord= qualityAcceptanceService.getOrderDetail(id);
        List<Record> listEntry=qualityAcceptanceService.getOrderRelateEntryList(id);
        model.addAttribute("detailRecord",detailRecord);
        model.addAttribute("listEntry",listEntry);
        return "modules/quality/qualityDetailPage";
    }

    //质检统计页面
    @RequestMapping(value = "qualityStatistics")
    public String qualityStatistics(Model model){
        List<Record> empList=platformEmpDao.getPlatformEmpList();
        model.addAttribute("empList",empList);
        model.addAttribute("nowMouth", DateUtils.getYear()+"-"+DateUtils.getMonth());
        return "modules/quality/qualityStatistics";
    }
    @RequestMapping(value = "inspectionStatistics")
    public String inspectionStatistics(Model model){
        List<Record> empList=platformEmpDao.getPlatformEmpList();
        model.addAttribute("empList",empList);
        model.addAttribute("nowMouth", DateUtils.getYear()+"-"+DateUtils.getMonth());
        return "modules/quality/inspectionStatistics";
    }
    @ResponseBody
    //质检统计页面
    @RequestMapping(value = "getQualityStatisticsList")
    public Object getQualityStatisticsList(HttpServletRequest request){
        Map<String, String> map = getParams(request);
        LayuiPage page = new LayuiPage(request);

        qualityAcceptanceService.getQualityStatisticsList(map,page);
        return page.renderJson();
    }
    //抽检验收页面
    @RequestMapping(value = "qualityAcceptPage")
    public String qualityAcceptPage(String id,Model model){
        Record detailRecord= qualityAcceptanceService.getOrderDetail(id);
        List<Record> listEntry=qualityAcceptanceService.getOrderEntryList(id,"","");  //电站信息（抽检的+未抽检的）
        model.addAttribute("detailRecord",detailRecord);
        model.addAttribute("listEntry",listEntry);
        return "modules/quality/qualityAcceptPage";
    }


    /**
     * 抽检验收页面 的提交 保存
     */
    @ResponseBody
    @RequestMapping(value = "saveQualityAccept")
    public Result saveQualityAccept(@Valid @RequestBody QualityAcceptVo qualityAcceptVo) {
        qualityAcceptanceService.saveQualityAccept(qualityAcceptVo);
        return Result.ok();
    }

    /**
     * 撤回电站验收
     */
    @ResponseBody
    @RequestMapping(value = "cancelAccept")
    public Result cancelAccept(String detailId){
        qualityAcceptanceService.cancelAccept(detailId);
        return Result.ok();
    }

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

    //无法验收的查看
    @RequestMapping(value = "canNotAcceptanceDetail")
    public String canNotAcceptanceDetail(String detailId,Model model){
        CrmEntryQualityAcceptanceDetail detail= qualityAcceptanceService.canNotAcceptanceDetail(detailId);
        model.addAttribute("detail",detail);
        return "modules/quality/canNotAcceptanceDetailPage";
    }

}
