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

import com.chinaitop.depot.intelligent.fumigation.model.*;
import com.chinaitop.depot.intelligent.fumigation.service.FumigationAfterService;
import com.chinaitop.depot.intelligent.fumigation.service.FumigationPlanService;
import com.chinaitop.depot.intelligent.fumigation.service.FumigationProcessService;
import com.chinaitop.depot.intelligent.fumigation.service.PesticidePlanService;
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-21 10:57
 */
@RestController
@RequestMapping(value = "/intelligents/fumigationAfter")
@Api(value = "FumigationAfterController", description = "熏蒸作业善后类")
@SuppressWarnings("all")
public class FumigationAlterController {

    @Resource
    FumigationAfterService fumigationAfterService;

    @Resource
    private FumigationPlanService fumigationPlanService;

    @Resource
    private FumigationProcessService fumigationProcessService;

    /**
     * 列表
     * @param pageNum
     * @param pageSize
     * @param houseId
     * @param orgId
     * @return
     */
    @RequestMapping(value = "/getFumigationAfterList", 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>> getFumigationAfterList(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 = fumigationAfterService.getFumigationAfterList(map);
        PageInfo<Map<String,Object>> pageInfo = new PageInfo<Map<String,Object>>(list);
        return pageInfo;
    }

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

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

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

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

        if (afterId != null) {
            TFumigationAfter tFumigationAfter = fumigationAfterService.findById(afterId);
            modelMap.put("afterEdit",tFumigationAfter);//熏蒸善后数据

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

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

        return modelMap;
    }

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

            TFumigationAfter tFumigationAfter = new TFumigationAfter();
            tFumigationAfter.setDeleteState("0");
            fumigationAfterService.updateByExample(tFumigationAfter, example);

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

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

    /**
     * 查询作业列表数据
     * @param id 主键id
     * @return
     */
    @RequestMapping(value="/getHomeWorkQueryList", produces = MediaType.APPLICATION_JSON_VALUE, 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>> getHomeWorkQueryList(Integer pageNum, Integer pageSize, Integer houseId, String orgId) {
        Map<String,Object> map = new HashMap<>();
        List<Map<String,Object>> list = null;
        map.put("houseId",houseId);
        map.put("orgId",orgId);

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

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