package com.chinaitop.depot.intelligent.fumigation.controller;

import com.chinaitop.depot.intelligent.fumigation.model.TFumigationPlan;
import com.chinaitop.depot.intelligent.fumigation.model.TFumigationPlanExample;
import com.chinaitop.depot.intelligent.fumigation.model.TFumigationProcess;
import com.chinaitop.depot.intelligent.fumigation.model.TFumigationProcessExample;
import com.chinaitop.depot.intelligent.fumigation.service.FumigationPlanService;
import com.chinaitop.depot.intelligent.fumigation.service.FumigationProcessService;
import com.chinaitop.depot.intelligent.grainsituation.model.TQtdevinfoValue;
import com.chinaitop.depot.intelligent.grainsituation.service.GasDetectionService;
import com.chinaitop.depot.intelligent.utils.JsonToObjectUtils;
import com.chinaitop.depot.intelligent.utils.ParameterUtil;
import com.chinaitop.depot.intelligent.utils.UuidUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lvzhikai
 * @description: 熏蒸作业记录
 * @create 2019-1-14 10:57
 */
@RestController
@RequestMapping(value = "/intelligents/fumigationProcess")
@Api(value = "FumigationProcessController", description = "熏蒸作业控制类")
@SuppressWarnings("all")
public class FumigationProcessController {

    @Resource
    private FumigationProcessService fumigationProcessService;

    @Resource
    private FumigationPlanService fumigationPlanService;

    @Resource
    private GasDetectionService gasDetectionService;

    /**
     * 列表
     * @param pageNum
     * @param pageSize
     * @param houseId
     * @param orgId
     * @return
     */
    @RequestMapping(value = "/getFumigationProcessList", method = RequestMethod.GET)
    @ApiOperation(value = "熏蒸作业列表", notes = "熏蒸作业列表，支持分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", paramType = "query"),
            @ApiImplicitParam(name = "houseId", value = "仓房Id", paramType = "query"),
            @ApiImplicitParam(name = "orgId", value = "组织机构编号", paramType = "query")
    })
    public PageInfo<Map<String,Object>> getFumigationProcessList(Integer pageNum, Integer pageSize, Integer houseId, Integer orgId) {
        Map<String,Object> map = new HashMap<>();
        List<Map<String,Object>> list = null;

        if (ParameterUtil.isnotnull(orgId))//粮库查询
            map.put("orgId",orgId);

        if (ParameterUtil.isnotnull(houseId))//粮库查询
            map.put("houseId",houseId);

        if (pageNum!=null && pageSize!=null) {
            PageHelper.startPage(pageNum, pageSize);
        }


        list = fumigationProcessService.getFumigationProcessList(map);
        PageInfo<Map<String,Object>> pageInfo = new PageInfo<Map<String,Object>>(list);
        return pageInfo;
    }

    /**
     * 保存熏蒸作业信息
     * @param fumigation
     * @return
     */
    @RequestMapping(value="/saveFumigationProcessDate", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
    @ApiOperation(value="保存熏蒸作业信息", notes = "保存熏蒸作业信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fumigation", value = "熏蒸作业数据", paramType = "form")
    })
    public Map<String, Object> saveFumigationProcessDate(String fumigationProcess) throws Exception {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        // JSON字符串转对象
        ObjectMapper mapper = new ObjectMapper();
        TFumigationProcess tFumigationProcess = (TFumigationProcess) JsonToObjectUtils.jsonToObject(fumigationProcess,"com.chinaitop.depot.intelligent.fumigation.model.TFumigationProcess");
        tFumigationProcess.setDeleteState("1");//默认设置删除标志
        if (tFumigationProcess.getId() == null) {
            tFumigationProcess.setCreateTime(new Date());//创建时间
            tFumigationProcess.setId(UuidUtils.getCode());
            fumigationProcessService.add(tFumigationProcess);

            //修改熏蒸状态
            TFumigationPlanExample example = new TFumigationPlanExample();
            TFumigationPlanExample.Criteria criteria = example.createCriteria();
            criteria.andIdEqualTo(tFumigationProcess.getFumigationId());

            TFumigationPlan tFumigationPlan = new TFumigationPlan();
            tFumigationPlan.setState(5);
            fumigationPlanService.updateByExample(tFumigationPlan, example);
        } else {
            tFumigationProcess.setUpdateTime(new Date());//修改时间
            fumigationProcessService.update(tFumigationProcess);
        }
        modelMap.put("status", "success");
        return modelMap;
    }

    /**
     * 根据id查询熏蒸作业
     * @param id 主键id
     * @return
     */
    @RequestMapping(value="/getProcessDeatil", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
    @ApiOperation(value="根据id查询熏蒸作业数据", notes = "根据id查询熏蒸作业数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "processId", value = "主键id", paramType = "query")
    })
    public Map<String, Object> getProcessDeatil(String processId) {
        Map<String, Object> modelMap = new HashMap<String, Object>();

        if (processId != null) {
            TFumigationProcess tFumigationProcess = fumigationProcessService.findById(processId);
            modelMap.put("processEdit",tFumigationProcess);//熏蒸作业过程数据

            TFumigationPlan tFumigationPlan = fumigationPlanService.findById(tFumigationProcess.getFumigationId());
            modelMap.put("fumigationEdit",tFumigationPlan);//熏蒸方案数据

            List<TQtdevinfoValue> tQtdevinfoValue = gasDetectionService.getList("","","","'"+tFumigationProcess.getDcsqtinfoId()+"'");
            modelMap.put("tQtdevinfoEdit",tQtdevinfoValue);//气体数据
        }

        return modelMap;
    }

    /**
     * 删除熏蒸作业信息
     * @param fumigationId 主键id
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/removeProcessPlan", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
    @ApiOperation(value="删除熏蒸作业信息", notes = "删除熏蒸作业信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "processId", value = "主键id", paramType = "form"),
            @ApiImplicitParam(name = "fumigationId", value = "熏蒸计划表ID", paramType = "form")
    })
    public Map<String, Object> removeProcessPlan(String processId,String fumigationId) throws Exception {
        Map<String, Object> modelMap = new HashMap<>();
        if (processId != null) {
            TFumigationProcessExample example = new TFumigationProcessExample();
            TFumigationProcessExample.Criteria criteria = example.createCriteria();
            criteria.andIdEqualTo(processId);

            TFumigationProcess tFumigationProcess = new TFumigationProcess();
            tFumigationProcess.setDeleteState("0");
            fumigationProcessService.updateByExample(tFumigationProcess, example);

            //修改熏蒸状态
            TFumigationPlanExample fumExample = new TFumigationPlanExample();
            TFumigationPlanExample.Criteria fumCriteria = fumExample.createCriteria();
            fumCriteria.andIdEqualTo(fumigationId);

            TFumigationPlan tFumigationPlan = new TFumigationPlan();
            tFumigationPlan.setState(4);
            fumigationPlanService.updateByExample(tFumigationPlan, fumExample);
            modelMap.put("status", "success");
        }
        return modelMap;
    }

    /**
     * 根据熏蒸计划id查询熏蒸作业
     * @param id 主键id
     * @return
     */
    @RequestMapping(value="/getFumIdByProcessDeatil", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
    @ApiOperation(value="根据熏蒸计划id查询熏蒸作业数据", notes = "根据熏蒸计划id查询熏蒸作业数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fumId", value = "熏蒸计划id", paramType = "query")
    })
    public Map<String, Object> getFumIdByProcessDeatil(String fumId) {
        Map<String, Object> modelMap = new HashMap<String, Object>();

        TFumigationProcessExample example = new TFumigationProcessExample();
        TFumigationProcessExample.Criteria criteria = example.createCriteria();
        criteria.andFumigationIdEqualTo(fumId);
        List<TFumigationProcess> tFumigationProcess = fumigationProcessService.getDataByFumId(example);
        modelMap.put("processEdit",tFumigationProcess);//熏蒸作业过程数据

        return modelMap;
    }
}
