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

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.chinaitop.depot.intelligent.fumigation.model.TFumigationPesticide;
import com.chinaitop.depot.intelligent.fumigation.model.TFumigationPesticideExample;
import com.chinaitop.depot.intelligent.fumigation.model.TFumigationPlan;
import com.chinaitop.depot.intelligent.fumigation.model.TFumigationPlanExample;
import com.chinaitop.depot.intelligent.fumigation.service.FumigationPlanService;
import com.chinaitop.depot.intelligent.fumigation.service.PesticidePlanService;
import com.chinaitop.depot.intelligent.utils.DateUtils;
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.apache.commons.lang3.StringUtils;
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 2018-12-14 10:57
 */
@RestController
@RequestMapping(value = "/intelligents/fumigationPlan")
@Api(value = "FumigationPlanController", description = "熏蒸计划控制类")
@SuppressWarnings("all")
public class FumigationPlanController {

    @Resource
    private FumigationPlanService fumigationPlanService;

    @Resource
    private PesticidePlanService pesticidePlanService;

    /**
     * 列表
     * @param pageNum
     * @param pageSize
     * @param houseId
     * @param orgId
     * @return
     */
    @RequestMapping(value = "/getFumigationPlanList", 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"),
            @ApiImplicitParam(name = "queryType", value = "查询状态", paramType = "query")
    })
    public PageInfo<TFumigationPlan> getFumigationPlanList(Integer pageNum, Integer pageSize, Integer houseId, String orgId, String queryType) {
        TFumigationPlanExample example = new TFumigationPlanExample();
        TFumigationPlanExample.Criteria criteria = example.createCriteria();

        Map<String,Object> map = new HashMap<>();

        if (ParameterUtil.isnotnull(houseId)) {//仓房查询
            criteria.andHouseIdEqualTo(houseId);
            map.put("houseId", houseId);
        }

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

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

        //施药数据。过程数据。善后数据
        if(ParameterUtil.isequal(queryType,"XZ") || ParameterUtil.isequal(queryType,"GC")){
            criteria.andStateEqualTo(4);
            map.put("state",4);
        }else if(ParameterUtil.isequal(queryType,"SH")){
            criteria.andStateEqualTo(5);
        }

        criteria.andDeleteStateEqualTo("1");
        example.setOrderByClause(" create_time DESC");
        map.put("deleteState",1);

        List<TFumigationPlan> list = null;
        if(ParameterUtil.isequal(queryType,"GC")){//熏蒸过程
            list = fumigationPlanService.getProcessList(map);
        }else{
            list = fumigationPlanService.getFumigationPlanList(example);
        }
        PageInfo<TFumigationPlan> pageInfo = new PageInfo<TFumigationPlan>(list);
        return pageInfo;
    }

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

        if (fumigationId != null) {
            TFumigationPlan tFumigationPlan = fumigationPlanService.findById(fumigationId);
            modelMap.put("fumigationEdit",tFumigationPlan);

            TFumigationPesticideExample example = new TFumigationPesticideExample();
            TFumigationPesticideExample.Criteria criteria = example.createCriteria();
            criteria.andFumigationIdEqualTo(fumigationId);
            List<TFumigationPesticide> list = pesticidePlanService.getFumigationPesticideList(example);
            modelMap.put("pesticideEdit",list.get(0));
        }

        return modelMap;
    }

    /**
     * 保存熏蒸方案申请信息
     * @param fumigation
     * @return
     */
    @RequestMapping(value="/saveFumigationPlanDate", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
    @ApiOperation(value="保存熏蒸方案申请信息", notes = "保存熏蒸方案申请信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fumigation", value = "熏蒸方案数据", paramType = "form"),
            @ApiImplicitParam(name = "pesticide", value = "施药方案数据", paramType = "form"),
            @ApiImplicitParam(name = "fumPlanId", value = "熏蒸方案Id", paramType = "form")
    })
    public Map<String, Object> saveFumigationPlanDate(String fumigation, String pesticide,String fumPlanId) throws Exception {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        // JSON字符串转对象
        ObjectMapper mapper = new ObjectMapper();
        TFumigationPlan tFumigationPlan = (TFumigationPlan) JsonToObjectUtils.jsonToObject(fumigation,"com.chinaitop.depot.intelligent.fumigation.model.TFumigationPlan");
        TFumigationPesticide tPesticidePlan = (TFumigationPesticide) JsonToObjectUtils.jsonToObject(pesticide,"com.chinaitop.depot.intelligent.fumigation.model.TFumigationPesticide");

        tFumigationPlan.setDeleteState("1");//默认设置删除标志
        tPesticidePlan.setDeleteState("1");//默认设置删除标志
        if (StringUtils.isBlank(tFumigationPlan.getId())) {
            tFumigationPlan.setCreateTime(new Date());//创建时间
            tFumigationPlan.setState(1);//待审批

            tFumigationPlan.setId(fumPlanId);//唯一id
            fumigationPlanService.add(tFumigationPlan);//熏蒸计划

            tPesticidePlan.setCreateTime(new Date());//创建时间
            tPesticidePlan.setState(1);//待审批
            tPesticidePlan.setFumigationId(fumPlanId);
            tPesticidePlan.setId(UuidUtils.getCode());
            pesticidePlanService.add(tPesticidePlan);//施药计划
            modelMap.put("fumigationId", tFumigationPlan.getId());
        } else {
            tFumigationPlan.setState(1);//待审批
            fumigationPlanService.update(tFumigationPlan);

            tPesticidePlan.setState(1);//待审批
            pesticidePlanService.update(tPesticidePlan);
        }
        modelMap.put("status", "success");
        return modelMap;
    }

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

            TFumigationPlan tFumigationPlan = new TFumigationPlan();
            tFumigationPlan.setDeleteState("0");
            fumigationPlanService.updateByExample(tFumigationPlan, example);

            //删除相关的施药计划
            TFumigationPesticideExample pesticideExample = new TFumigationPesticideExample();
            TFumigationPesticideExample.Criteria pesticideCriteria = pesticideExample.createCriteria();
            pesticideCriteria.andFumigationIdEqualTo(fumigationId);

            TFumigationPesticide TFumigationPesticide = new TFumigationPesticide();
            TFumigationPesticide.setDeleteState("0");
            pesticidePlanService.updateByExample(TFumigationPesticide, pesticideExample);
            modelMap.put("status", "success");
        }
        return modelMap;
    }

    /**
     * 列表
     * @param pageNum
     * @param pageSize
     * @param houseId
     * @param orgId
     * @return
     */
    @RequestMapping(value = "/getFumigationApproveList", 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"),
            @ApiImplicitParam(name = "userId", value = "当前登陆人id", paramType = "query")
    })
    public PageInfo<Map<String,Object>> getFumigationApproveList(Integer pageNum, Integer pageSize, Integer houseId, String orgId, String userId) {
        Map<String,Object> map = new HashMap<>();
        List<Map<String,Object>> list = null;
        map.put("houseId",houseId);
        map.put("orgId",orgId);
        map.put("userId",userId);

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

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

    /**
     * 保存熏蒸方案审批信息
     * @param fumigation
     * @return
     */
    @RequestMapping(value="/saveFumigationApproveDate", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
    @ApiOperation(value="保存熏蒸方案审批信息", notes = "保存熏蒸方案审批信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fumigation", value = "熏蒸方案数据", paramType = "form"),
            @ApiImplicitParam(name = "pesticide", value = "施药方案数据", paramType = "form")
    })
    public Map<String, Object> saveFumigationApproveDate(String fumigation,String pesticide) throws Exception {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        // JSON字符串转对象
        ObjectMapper mapper = new ObjectMapper();
        TFumigationPlan tFumigationPlan = (TFumigationPlan) JsonToObjectUtils.jsonToObject(fumigation,"com.chinaitop.depot.intelligent.fumigation.model.TFumigationPlan");
        TFumigationPesticide tPesticidePlan = (TFumigationPesticide) JsonToObjectUtils.jsonToObject(pesticide,"com.chinaitop.depot.intelligent.fumigation.model.TFumigationPesticide");

        tFumigationPlan.setApproveTime(new Date());
        tPesticidePlan.setApproveTime(new Date());

        fumigationPlanService.update(tFumigationPlan);
        pesticidePlanService.update(tPesticidePlan);
        modelMap.put("status", "success");
        return modelMap;
    }

    /**
     * 查询方案列表数据
     * @param id 主键id
     * @return
     */
    @RequestMapping(value="/getPlanQueryList", 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>> getPlanQueryList(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 = fumigationPlanService.selectPlanQueryList(map);
        PageInfo<Map<String,Object>> pageInfo = new PageInfo<Map<String,Object>>(list);
        return pageInfo;
    }

    /**
     * 作业全部信息--省级平台用
     * @param id 主键id
     * @return
     */
    @RequestMapping(value="/getAllQueryList", 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 = "orgId", value = "组织机构编号", paramType = "query")
    })
    public PageInfo<Map<String,Object>> getAllQueryList(Integer pageNum, Integer pageSize, String orgId) {
        Map<String,Object> map = new HashMap<>();
        List<Map<String,Object>> list = null;
        map.put("orgId",orgId);

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

        list = fumigationPlanService.getAllQueryList(map);
        PageInfo<Map<String,Object>> pageInfo = new PageInfo<Map<String,Object>>(list);
        return pageInfo;
    }
    
    /**
	 * 获取害虫检测单号
	 * @param houseId	仓房id
	 * @param fumigationId	熏蒸计划id
	 * @return
	 */
	@RequestMapping(value="/getJcd", method = RequestMethod.GET)
	@ApiOperation(value="获取害虫检测单号", notes = "获取害虫检测单号")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "houseId", value = "仓房id", paramType = "query"),
			@ApiImplicitParam(name = "fumigationId", value = "熏蒸计划id", paramType = "query")
	})
	public List<Map<Object, Object>> getJcd(String houseId,String fumigationId) {
		Map<Object, Object> map = new HashMap<>();
		if (StringUtils.isNotBlank(houseId)) {
			map.put("houseId", houseId);
		}
		if (StringUtils.isNotBlank(fumigationId)) {
			map.put("fumigationId", fumigationId);
		}
		List<Map<Object, Object>> listMap = fumigationPlanService.getJcd(map);
		return listMap;
	}
	
	/**
     * 熏蒸方案状态提交
     *
     * @param id 数据ID
     * @return
     */
    @RequestMapping(value="/submit",produces = MediaType.APPLICATION_JSON_VALUE, method=RequestMethod.POST)
    @ApiOperation(value="熏蒸方案提交", notes = "熏蒸方案提交")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "数据ID", paramType = "form"),
            @ApiImplicitParam(name = "approvalStatus", value = "状态", paramType = "form")
    })
    public Map<String, Object> submit(String id, Integer approvalStatus) {
        Map<String,Object> modelMap = new HashMap<>();
        try {
            fumigationPlanService.submit(id, approvalStatus);
        } catch (Exception e) {
            e.printStackTrace();
        }

        modelMap.put("status", "success");
        return modelMap;
    }
}
