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

import com.chinaitop.depot.intelligent.ventilation.model.StorageFoodbasicinfo;
import com.chinaitop.depot.intelligent.ventilation.model.TJobApplication;
import com.chinaitop.depot.intelligent.ventilation.model.TJobApplicationAudit;
import com.chinaitop.depot.intelligent.ventilation.service.AerationTaskService;
import com.chinaitop.depot.intelligent.ventilation.service.StorageFoodbasicInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.unissoft.model.ResponseEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
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 java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/intelligents/aerationTask")
@Api(value = "AerationTaskController", description = "通风计划申请类")
public class AerationTaskController {

    @Autowired
    private AerationTaskService aerationTaskService;
    
    @Autowired
    private StorageFoodbasicInfoService foodbasicInfoService;


    /**
     * 列表
     * @param pageNum
     * @param pageSize
     * @param state
     * @return
     */
    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    @ApiOperation(value = "查询 通风记录列表", notes = "查询 通风记录列表，支持分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "通风状态", paramType = "query")
    })
    public ResponseEntity<PageInfo<TJobApplication>> getList(Integer pageNum, Integer pageSize, TJobApplication tJobApplication , Integer approvalState,Integer userId) {
        List<TJobApplication> list = null;
        try {
            if (null != pageNum && null != pageSize) {
                PageHelper.startPage(pageNum, pageSize);
            }
            list = aerationTaskService.getList(tJobApplication,approvalState,userId);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseEntity.failed(e.getMessage());
        }
        PageInfo<TJobApplication> pageInfo = new PageInfo<TJobApplication>(list);
        return ResponseEntity.ok(pageInfo);
    }

    /**
     * 新增
     */
    @RequestMapping(value = "/save", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
    @ApiOperation(value = "数据更新与添加", notes = "数据更新与添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskApplyJson", value = "表数据json串", paramType = "form")
    })
    public ResponseEntity save(String taskApplyJson,Integer orgId) {
        String id = "";
        try {
            Map<String, String> returnMap = aerationTaskService.save(taskApplyJson,orgId);
            id = returnMap.get("id");
        } catch (Exception e) {
            e.printStackTrace();
            ResponseEntity.failed(e.getMessage());
        }
        return ResponseEntity.ok(id);
    }
    
    
    /**
     * 通风申请提交
     * 
     * @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 ResponseEntity submit(String id, Integer approvalStatus) {
    	try {
    		 aerationTaskService.submit(id, approvalStatus);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseEntity.failed(e.getMessage());
        }
       
        return ResponseEntity.ok();
    }
    
    /**
     * 根据id删除数据
     * @param id
     * @return
     */
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    @ApiOperation(value = "根据id删除通风计划申请", notes = "根据id删除通风计划申请")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "通风计划id", paramType = "query")
    })
    public ResponseEntity remove(String id) {
        if (StringUtils.isNotBlank(id)) {
            int count = aerationTaskService.deleteById(id);
            if (count > 0) {
                ResponseEntity.ok();
                return ResponseEntity.ok();
            } else {
                return ResponseEntity.failed("删除失败!");
            }
        }
        return ResponseEntity.failed("ID数据有误!");
    }
    
    
    /**
     * 通风审批分页查询
     * 
     * @param pageNum 页数
     * @param pageSize 每页显示的条数
     * @param applyTime 申请时间
     * @param taskId 通风作业编号
     * @param taskStatus 作业状态
     */
    @RequestMapping(value = "/getApprovalList",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 = "tJobApplicationAudit", value = "查询条件", paramType = "query"),
        @ApiImplicitParam(name = "userId", value = "当前登录人id", paramType = "query")
    })
    public ResponseEntity<PageInfo<TJobApplicationAudit>> getApprovalList(Integer pageNum, Integer pageSize, TJobApplicationAudit tJobApplicationAudit ,Integer userId){
        //TJobApplicationExample tJobApplicationExample = new TJobApplicationExample();
		//TJobApplicationExample.Criteria criteria = tJobApplicationExample.createCriteria();
		
		
        /*List<String> id_list = null;
        if (null != ids && !"".equals(ids)) {
            id_list = new ArrayList<String>();
            JSONArray json = JSON.parseArray(ids);
            Iterator<Object> it = json.iterator();
            while(it.hasNext()){
                JSONObject sObj = (JSONObject)it.next();
                String id = (String) sObj.get("id");
                id_list.add(id);
            }
        }
 
        //查询当前所在的仓的信息
        if (null != pageNum && null != pageSize) {
            PageHelper.startPage(pageNum, pageSize);
        }
        //tJobApplicationExample.setOrderByClause("id desc");
 
         //查询保管科长和库主任待审批订的数据 
       // criteria.andStateOrEqual(2, 3);
        
        List<TJobApplicationAudit> list = null;
        
 
        if (null != id_list && id_list.size() > 0) {
            criteria.andIdIn(id_list);
            paramMap.put(key, value)
            //查询当前所在的仓的信息(id_list必须不为空，否则查询无意义)
            if (null != pageNum && null != pageSize) {
                PageHelper.startPage(pageNum, pageSize);
            }
            list = aerationTaskService.findCondition(tJobApplicationExample);
        } else {
            list = new ArrayList<TJobApplicationAudit>();
        }
        PageInfo<TJobApplicationAudit> pageInfo = new PageInfo<TJobApplicationAudit>(list);
        */
        
        
        PageHelper.startPage(pageNum, pageSize);
        
        
        Map<String,Object> paramMap = new HashMap<String, Object>();
        paramMap.put("aid", userId);
        paramMap.put("userId", userId);
        if(StringUtils.isNotEmpty(tJobApplicationAudit.getResult())&&tJobApplicationAudit.getResult()!=null){
        	String result = tJobApplicationAudit.getResult();
        	paramMap.put("result", result);
		}
        List<TJobApplicationAudit> list = aerationTaskService.getApprovalList(paramMap);
        PageInfo<TJobApplicationAudit> pageInfo = new PageInfo<TJobApplicationAudit>(list);
        
        return ResponseEntity.ok(pageInfo);
    }
    
    
    /**
     * 根据id查找数据
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ApiOperation(value = "根据ID查询一条数据", notes = "根据ID查询一条数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "粮情id", paramType = "query")
    })
    public ResponseEntity<TJobApplication> getById(String id) {
    	TJobApplication tJobApplication = null;
        if (StringUtils.isNotBlank(id)) {
        	tJobApplication = aerationTaskService.getById(id);
        } else {
            return ResponseEntity.failed("ID数据有误!");
        }
        return ResponseEntity.ok(tJobApplication);
    }
    
    
    
    @RequestMapping(value = "/getJobCtrlList", method = RequestMethod.GET)
    @ApiOperation(value = "查询 通风记录列表", notes = "查询 通风记录列表，支持分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "通风状态", paramType = "query")
    })
    public ResponseEntity<PageInfo<TJobApplication>> getJobCtrlList(Integer pageNum, Integer pageSize,TJobApplication tJobApplication) {
        List<TJobApplication> list = null;
        try {
            if (null != pageNum && null != pageSize) {
                PageHelper.startPage(pageNum, pageSize);
            }
            Map<String,String> map = new HashMap<String,String>();
            String vcfCode = null;
            if(null != tJobApplication.getvCfCode()  && tJobApplication.getvCfCode() != ""){
            	 vcfCode = tJobApplication.getvCfCode();
            }
            map.put("vcfCode", vcfCode);
            list = aerationTaskService.getJobCtrlList(map);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseEntity.failed(e.getMessage());
        }
        PageInfo<TJobApplication> pageInfo = new PageInfo<TJobApplication>(list);
        return ResponseEntity.ok(pageInfo);
    }
    
    
    /**
     * 根据计划编号查找数据
     * @param getHouseByNumber
     * @return
     */
    @RequestMapping(value = "/getHouseByNumber", method = RequestMethod.POST)
    @ApiOperation(value = "根据计划编号查询一条数据", notes = "根据计划编号查询一条数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "计划编号", value = "aerationPlanNumber", paramType = "query")
    })
    public ResponseEntity<TJobApplication> getHouseByNumber(String aerationPlanNumber) {
    	TJobApplication tJobApplication = null;
        if (StringUtils.isNotBlank(aerationPlanNumber)) {
        	tJobApplication = aerationTaskService.getHouseByNumber(aerationPlanNumber);
        } else {
            return ResponseEntity.failed("ID数据有误!");
        }
        return ResponseEntity.ok(tJobApplication);
    }
    
    /**
     * 得到当前仓的粮情专卡
     * @return
     */
    @RequestMapping(value="/getFoodbasicInfoByHouseId",produces = MediaType.APPLICATION_JSON_VALUE,method = RequestMethod.GET)
    @ApiOperation(value="根据仓号,货位号查找对应粮情专卡", notes = "根据仓号,货位号查找对应粮情专卡")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "houseId", value = "仓房ID", paramType = "query"),
            @ApiImplicitParam(name = "warehouseId", value = "货位ID", paramType = "query")
    })
    public List<StorageFoodbasicinfo> getFoodbasicInfoByHouseId(Integer houseId, Integer warehouseId){
    	StorageFoodbasicinfo foodbasicinfo = new StorageFoodbasicinfo();
        if(houseId != null) {
            foodbasicinfo.setHouseId(houseId);
        }
        if(warehouseId != null) {
            foodbasicinfo.setWarehouseId(warehouseId);
        }
    	List<StorageFoodbasicinfo>  list = foodbasicInfoService.getByHouseIdAndWarehouseId(foodbasicinfo);
       
    	return list;
    }
}
