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.vo.DispatchEmpVo;
import com.sifang.modules.quality.vo.ListSortingVo;
import com.sifang.modules.quality.vo.QualityAcceptVo;
import com.sifang.modules.quality.vo.SaveAcceptItemVo;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
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.RestController;

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

//质检验收
@RestController
@RequestMapping(value = "${adminPath}/main/quality")
public class SmallQualityController extends BaseExceptionController {

    @Autowired
    private CrmEntryQualityAcceptanceService qualityAcceptanceService;

    @Autowired
    private CrmEntryQualityAcceptanceResultService acceptanceResultService;

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

    //派工数据获取
    @RequestMapping(value = "getDispatchEmp")
    public Result getDispatchEmp(HttpServletRequest request){
        Map<String, String> map = getParams(request);
        List<Record> list=qualityAcceptanceService.getDispatchEmpData(map);
        return Result.success(list);
    }

    //保存派工
    @RequestMapping(value = "saveDispatch")
    public Result saveDispatch(DispatchEmpVo req,HttpServletRequest request){
        Operator operator = getOperatorBy(request);
        qualityAcceptanceService.saveDispatchEmp(req,operator);
        return Result.ok();
    }

    //详情
    @RequestMapping(value = "qualityDetailData")
    public Result qualityDetailData(String id,String tenantName){
        Record detailRecord= qualityAcceptanceService.getOrderDetail(id);
        Map<String,Object> resultMap=new HashMap<>();
        resultMap.put("detailRecord",detailRecord);
        //抽检的电站
        List<Record> cjEntry=qualityAcceptanceService.getOrderEntryList(id,"1,2",tenantName);
        //电站列表
        List<Record> wcjEntry=qualityAcceptanceService.getOrderEntryList(id,"0,3","");
        resultMap.put("cjEntry",cjEntry);
        resultMap.put("wcjEntry",wcjEntry);
        return Result.success(resultMap);
    }

    //无法验收
    @RequestMapping(value = "canNotAcceptance")
    public Result canNotAcceptance(String detailId,HttpServletRequest request){
        String reason=request.getParameter("reason");
        String imgPath=request.getParameter("imgPath");
        qualityAcceptanceService.canNotAcceptance(detailId,reason,imgPath);
        return Result.ok();
    }

    //撤回
    @RequestMapping(value = "cancelAccept")
    public Result cancelAccept(String detailId,HttpServletRequest request){
        qualityAcceptanceService.cancelAccept(detailId);
        return Result.ok();
    }

    //添加抽检
    @RequestMapping(value = "addSample")
    public Result addSample(String id,String detailId){
        QualityAcceptVo acceptVo=new QualityAcceptVo();
        acceptVo.setId(id);
        List<String> list= Arrays.asList(detailId);
        acceptVo.setDetails(list);
        qualityAcceptanceService.saveQualityAccept(acceptVo);
        return Result.ok();
    }

    /**
     * 质检验收数据（暂存的包含了暂存的信息）
     */
    @RequestMapping(value = "acceptItems")
    public Result acceptItems(String detailId){
        List<Record> list=acceptanceResultService.acceptItems(detailId);
        Record count=acceptanceResultService.getResultCount(detailId);
        Map<String,Object> resultMap=new HashMap<>();
        resultMap.put("itemList",list);
        resultMap.put("countRecord",count);
        return Result.success(resultMap);
    }

    //列表排序
    @RequestMapping(value = "listSorting")
    public Result listSorting(ListSortingVo req){
        return Result.success( qualityAcceptanceService.listSorting(req));
    }
    /**
     * 保存验收信息
     */
    @RequestMapping(value = "saveAcceptItem")
    public Result saveAcceptItem(@RequestBody SaveAcceptItemVo itemVo){
        acceptanceResultService.saveAcceptItem(itemVo);
        return Result.ok();
    }

    //无法验收的查看
    @RequestMapping(value = "canNotAcceptanceDetail")
    public Result canNotAcceptanceDetail(String detailId){
        CrmEntryQualityAcceptanceDetail detail= qualityAcceptanceService.canNotAcceptanceDetail(detailId);
        return  Result.success(detail);
    }

}
