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

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.service.FumigationPlanService;
import com.chinaitop.depot.intelligent.fumigation.service.PesticidePlanService;
import com.chinaitop.depot.intelligent.utils.JsonToObjectUtils;
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 2018-12-14 10:57
 */
@RestController
@RequestMapping(value = "/intelligents/pesticidePlan")
@Api(value = "PesticidePlanController", description = "施药计划控制类")
@SuppressWarnings("all")
public class PesticidePlanController {

    @Resource
    private PesticidePlanService pesticidePlanService;

    @Resource
    private FumigationPlanService fumigentionPlanService;

    /**
     * 列表
     * @param pageNum
     * @param pageSize
     * @param houseId
     * @param orgId
     * @return
     */
    @RequestMapping(value = "/getPesticidePlanList", 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 = "planType", value = "查询状态", paramType = "query")
    })
    public PageInfo<Map<String,Object>> getPesticidePlanList(Integer pageNum, Integer pageSize, Integer houseId, Integer orgId, String planType) {
        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 = pesticidePlanService.getPesticidePlanList(map);
        PageInfo<Map<String,Object>> pageInfo = new PageInfo<Map<String,Object>>(list);
        return pageInfo;
    }

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

        if (pesticideId != null) {
            TFumigationPesticide TFumigationPesticide = pesticidePlanService.findById(pesticideId);
            modelMap.put("pesticideEdit",TFumigationPesticide);

            TFumigationPlan tFumigationPlan = fumigentionPlanService.findById(TFumigationPesticide.getFumigationId());
            modelMap.put("fumigationEdit",tFumigationPlan);
        }

        return modelMap;
    }

    /**
     * 列表
     * @param fumigation
     * @return
     */
    @RequestMapping(value="/savePesticidePlanDate", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
    @ApiOperation(value="保存施药方案申请信息", notes = "保存施药方案申请信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fumigation", value = "施药方案数据", paramType = "form")
    })
    public Map<String, Object> savePesticidePlanDate(String pesticide) throws Exception {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        // JSON字符串转对象
        ObjectMapper mapper = new ObjectMapper();
        TFumigationPesticide TFumigationPesticide = (TFumigationPesticide) JsonToObjectUtils.jsonToObject(pesticide,"com.chinaitop.depot.intelligent.fumigation.model.TFumigationPesticide");
        TFumigationPesticide.setDeleteState("1");//默认设置删除标志
        if (TFumigationPesticide.getId() == null) {
            TFumigationPesticide.setCreateTime(new Date());//创建时间
            TFumigationPesticide.setState(1);//待审批
            TFumigationPesticide.setId(UuidUtils.getCode());
            pesticidePlanService.add(TFumigationPesticide);
            modelMap.put("pesticideId", TFumigationPesticide.getId());
        } else {
            TFumigationPesticide.setState(1);//待审批
            pesticidePlanService.update(TFumigationPesticide);
        }
        modelMap.put("status", "success");
        return modelMap;
    }

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

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

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

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

    /**
     * 根据熏蒸id查询施药数据
     * @param id 主键id
     * @return
     */
    @RequestMapping(value="/geByFumigationId", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
    @ApiOperation(value="根据熏蒸id查询施药数据", notes = "根据熏蒸id查询施药数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fumigationId", value = "熏蒸id", paramType = "query")
    })
    public List<TFumigationPesticide> geByFumigationId(String fumigationId) {
        TFumigationPesticideExample example = new TFumigationPesticideExample();
        TFumigationPesticideExample.Criteria criteria = example.createCriteria();
        criteria.andFumigationIdEqualTo(fumigationId);

        List<TFumigationPesticide> tFumigationPesticides = pesticidePlanService.getFumigationPesticideList(example);

        return tFumigationPesticides;
    }

    /**
     * 施药方案状态提交
     *
     * @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 {
            pesticidePlanService.submit(id, approvalStatus);
        } catch (Exception e) {
            e.printStackTrace();
        }

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