package com.unis.base.server.controller;

import java.util.List;

import com.unis.base.common.vo.DSbxxsjyClassifyVO;
import com.unis.base.common.vo.DSbxxsjyQueryVO;
import com.unis.base.common.vo.EquipmentRecordsVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.unis.common.utils.ResponseWrapper;
import com.unis.base.common.vo.DSbxxsjyVO;
import com.unis.base.server.entity.DSbxxsjyDO;
import com.unis.base.server.service.DSbxxsjyService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * @author huangxing
 * @version 1.0
 * @Title DSbxxsjyController
 * @ProjectName 设备信息 Controller
 * @Description
 * @date 2019/07/22 17点26分
 */
@RestController
@Api(value = "设备信息", tags = "设备信息维护")
@Slf4j
public class EquipmentController {

	@Autowired
	private DSbxxsjyService dSbxxsjyService;

	/**
	 * @author huangxing
	 * @Description 获取设备列表
	 * @param 设备编号，设备名称，库点编号，库点名称，设备类型，设备状态, 机构ID
	 * @return 设备信息列表
	 */
	@ApiOperation(value = "获取设备信息列表", notes = "参数：设备编号，设备名称，库点编号，库点名称，设备类型，设备状态, 机构ID")
	@PostMapping(value = "/equipmentList")
    public ResponseWrapper getDSbxxsjyList(@RequestBody DSbxxsjyQueryVO dSbxxsjyQueryVO){
        try {
        	List<DSbxxsjyVO> dSbxxsjyDOs = dSbxxsjyService.getDSbxxsjyList(dSbxxsjyQueryVO);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), dSbxxsjyDOs);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }

	/**
	 * @author huangxing
	 * @Description 获取设备信息列表
	 * @param 设备编号，设备名称，库点编号，库点名称，设备类型，设备状态, 机构ID
	 * @return 设备信息列表
	 */
	@ApiOperation(value = "分页获取设备信息列表", notes = "参数：设备编号，设备名称，库点编号，库点名称，设备类型，设备状态, 机构ID")
	@PostMapping(value = "/equipmentPage/{pageNum}/{pageSize}")
	public ResponseWrapper getDSbxxsjyPage(@RequestBody DSbxxsjyQueryVO dSbxxsjyQueryVO,@PathVariable("pageNum")Integer pageNum,@PathVariable("pageSize") Integer pageSize) {
		try {
			PageInfo<DSbxxsjyVO> pageInfos = new PageInfo<DSbxxsjyVO>(dSbxxsjyService.getDSbxxsjyPage(dSbxxsjyQueryVO,pageNum,pageSize)) ;
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), pageInfos);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
		return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
	}

    /**
     * @author WangZL
     * @Description 获取设备列表
     * @param 机构ID
     * @return 设备信息列表
     */
    @ApiOperation(value = "根据机构获取设备信息列表", notes = "参数：机构ID")
    @PostMapping(value = "/equipmentList/{jgId}")
    public ResponseWrapper getDSbxxsjyListByJgId(@PathVariable("jgId")String jgId){
        try {
            if (jgId == null) {
                return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
            }
            List<DSbxxsjyVO> dSbxxsjyList = dSbxxsjyService.getDSbxxsjyListByJgId(jgId);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), dSbxxsjyList);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }

	/**
	 * @author WangZL
	 * @Description 根据库点获取设备信息列表
	 * @param 库点代码
	 * @return 设备信息列表
	 */
	@ApiOperation(value = "根据库点获取设备信息列表", notes = "参数：库点代码")
	@PostMapping(value = "/equipmentListByKddm/{kddm}")
	public ResponseWrapper getEquipmentListByKddm(@PathVariable("kddm")String kddm){
		try {
			if (kddm == null) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			List<DSbxxsjyVO> dSbxxsjyList = dSbxxsjyService.getEquipmentListByKddm(kddm);
			return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), dSbxxsjyList);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}
		return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
	}


	/**
	 * @author huangxing
	 * @Description 根据设备编码获取设备信息
	 * @return 库点信息
	 */
	@ApiOperation(value = "根据设备编码获取设备信息", notes = "参数：库点编号")
	@GetMapping(value = "/equipment/{sbbh}")
	public ResponseWrapper getDSbxxsjyInfoBysbbh(@PathVariable("sbbh")String sbbh) {
		try {
			DSbxxsjyDO dKdxxsjyDO = dSbxxsjyService.getDSbxxsjyInfoBysbbh(sbbh);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), dKdxxsjyDO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
	}

	/**
	 * @author huangxing
	 * @Description 根据库点编码获取库点信息
	 * @return 库点信息
	 */
	@ApiOperation(value = "更新设备信息", notes = "参数：设备编号")
	@PutMapping(value = "/equipment")
	public ResponseWrapper update(@RequestBody DSbxxsjyVO dSbxxsjyVO) {
		try{
			if(StringUtils.isEmpty(dSbxxsjyVO.getSbbh())) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			int x = dSbxxsjyService.update(dSbxxsjyVO);
			if(x>0) {
	            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(),null);
			} else if (x==0) {
	            return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "该设备不存在", null);
			}
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
	}


	/**
	 * @author huangxing
	 * @Description 根据库点编码获取库点信息
	 * @return 库点信息
	 */
	@ApiOperation(value = "批量更新设备信息", notes = "参数：设备编号")
	@PutMapping(value = "/equipmentBatch")
	@Transactional
	public ResponseWrapper updateBatch(@RequestBody DSbxxsjyVO dSbxxsjyVO) {
		try{
			if(dSbxxsjyVO.getSbbhs()==null||dSbxxsjyVO.getSbbhs().isEmpty()) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}

			List<String> sbbhs = dSbxxsjyVO.getSbbhs();

			for (String sbbh : sbbhs) {

				dSbxxsjyVO.setSbbh(sbbh);

				int x = dSbxxsjyService.update(dSbxxsjyVO);

				if (x==0) {
		            return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "该设备不存在", null);
				}

			}

	            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(),null);

        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
	}
	/**
	 * @author WangZL
	 * @Description 根据库点编码获取库点信息
	 * @return 库点信息
	 */
	@ApiOperation(value = "设备信息-带状态更新", notes = "参数：设备编号")
	@PutMapping(value = "/equipmentUpdate")
	public ResponseWrapper equipmentUpdate(@RequestBody DSbxxsjyVO dSbxxsjyVO) {
		try{
			if(StringUtils.isEmpty(dSbxxsjyVO.getSbbh())) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			int x = dSbxxsjyService.equipmentUpdate(dSbxxsjyVO);
			if(x>0) {
				return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(),null);
			} else if (x==0) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "该设备不存在", null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}
		return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
	}

	/**
	 * @author huangxing
	 * @Description 删除企业信息
	 * @param 参数：社会行政代码
	 * @return 状态
	 */
	@DeleteMapping(value="/equipment/{sbbh}")
	@ApiOperation(value = "根据设备编号删除设备信息", notes = "参数：设备编号")
	public ResponseWrapper delete(@PathVariable("sbbh")String sbbh) {
		try{
			int x = dSbxxsjyService.delete(sbbh);
			if(x>0) {
	            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(),null);
			} else if (x==0) {
	            return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "删除失败", null);
			} else {
				 return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "删除失败,请设备编号是否正确", null);
			}
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }

	/**
	 * @author WangZL
	 * @Description 设备信息-根据条件删除设备列表
	 * @param 库点代码，设施分类
	 * @return 状态
	 */
	@DeleteMapping(value="/equipmentDeleteList")
	@ApiOperation(value = "设备信息-根据条件删除设备列表", notes = "参数：库点代码，设施分类")
	public ResponseWrapper deleteList(@RequestBody DSbxxsjyClassifyVO dSbxxsjyClassifyVO) {
		try{
			if(StringUtils.isEmpty(dSbxxsjyClassifyVO.getKddm())) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			int x = dSbxxsjyService.deleteList(dSbxxsjyClassifyVO);
			if(x>0) {
				return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(),null);
			} else if (x==0) {
				return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), null);
			} else {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "删除失败,请库点代码是否正确", null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}
		return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
	}

	/**
	 * @author huangxing
	 * @Description 新增设备信息
	 * @param 设备信息实体 models
	 * @return 新增状态
	 */
	@PostMapping(value = "/equipment")
	@ApiOperation(value = "新增设备信息", notes = "参数：设备仪器名称，库点代码")
    public ResponseWrapper insert(@RequestBody DSbxxsjyVO dSbxxsjyVO) {
		try{
			/*if(StringUtils.isEmpty(dSbxxsjyVO.getSbyqmc())||StringUtils.isEmpty(dSbxxsjyVO.getKddm())) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}*/
			int x = dSbxxsjyService.insert(dSbxxsjyVO);
			if(x>0) {
	            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(),null);
			} else if (x==0) {
	            return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "添加设备失败,该设备已存在", null);
			} else if (x==-1) {
	            return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "添加设备失败,该库点不存在", null);
			}
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }

	/**
	 * 设备信息-更改备案状态
	 * @param equipmentRecordsVO
	 * @return
	 */
	@PostMapping(value = "/equipmentFilingStatus")
	@ApiOperation(value = "设备信息-更改备案状态", notes = "参数：备案信息")
	public ResponseWrapper updateFilingStatus(@RequestBody EquipmentRecordsVO equipmentRecordsVO) {
		try {
			if (equipmentRecordsVO.getSfba() == null) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			Integer i = dSbxxsjyService.updateFilingStatus(equipmentRecordsVO);
			if (i > 0) {
				return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), "操作成功");
			} else if (i==-1) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "只有已备案和已驳回才能添加记录", null);
			} else if (i==-2) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "请输入驳回审批意见", null);
			} else {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.getReasonPhrase(), "操作失败，请联系管理员");
			}
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), "服务错误，请联系管理员");
		}
	}

	/**
	 * 设备信息-备案主表状态
	 * @param dSbxxsjyVO
	 * @return
	 */
	@PostMapping(value = "/equipmentFiling")
	@ApiOperation(value = "设备信息-备案主表状态", notes = "参数：设备编号，是否备案")
	public ResponseWrapper updateFiling(@RequestBody DSbxxsjyVO dSbxxsjyVO) {
		try {
			if (StringUtils.isEmpty(dSbxxsjyVO.getSbbh()) || dSbxxsjyVO.getSfba() == null) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			int i = dSbxxsjyService.updateFiling(dSbxxsjyVO);
			if (i > 0) {
				return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), "操作成功");
			} else {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.getReasonPhrase(), "操作失败，请联系管理员");
			}
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), "服务错误，请联系管理员");
		}
	}

	@Transactional
	@PostMapping(value = "/equipmentBatch")
	@ApiOperation(value = "新增设备信息", notes = "参数：设备仪器名称，库点代码")
    public ResponseWrapper insertBatch(@RequestBody DSbxxsjyVO dSbxxsjyVO) {
		try{
			/*if(StringUtils.isEmpty(dSbxxsjyVO.getSbyqmc())||StringUtils.isEmpty(dSbxxsjyVO.getKddm())) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}*/

			Integer sbsl = dSbxxsjyVO.getSbsl();

			if(sbsl==null||sbsl<=0) {

				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.getReasonPhrase(), "设备数量不能为空或小于0");
			}
			dSbxxsjyVO.setSbsl(1);
			for(int i=0;i<sbsl;i++) {

				int x = dSbxxsjyService.insert(dSbxxsjyVO);
				if (x==0) {
		            return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "添加设备失败,该设备已存在", null);
				} else if (x==-1) {
		            return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "添加设备失败,该库点不存在", null);
				}
			}
			       return ResponseWrapper.success("新增成功！");


			} catch (Exception e) {
				log.error(e.getMessage());
				return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), "服务错误，请联系管理员");
			}
    }

}
