package com.unis.base.server.controller;

import com.github.pagehelper.PageInfo;
import com.unis.base.common.vo.*;
import com.unis.base.server.service.DYpxxService;
import com.unis.common.utils.ResponseWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author ：yangshengjie
 * @date ：Created in 2019/9/20 13:42
 */
@RestController
@Slf4j
@Api(value = "药剂信息",tags = "药剂信息查询")
public class MedicineinfoController {

    @Autowired
    private DYpxxService dYpxxService;

    /**
     * create by: yangshengjie
     * create time: 2019/9/20 13:44
     *
     * @Param: 所属库名，药剂名称，月份
     * @return 药剂信息列表
     */
    @PostMapping("/findDYpxxList/{pageNum}/{pageSize}")
    @ApiOperation(value = "分页获取药剂信息列表",notes = "参数：所属库名，药剂名称，月份")
    public ResponseWrapper findDYpxxList(@RequestBody DYpxxQueryVO dYpxxQueryVO,
    		@PathVariable(value = "pageNum") Integer pageNum, @PathVariable(value = "pageSize") Integer pageSize){
        try {
            PageInfo<DYpxxVO> pageInfo =dYpxxService.findDYpxxList(dYpxxQueryVO, pageNum, pageSize);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), pageInfo);
        }catch (Exception e){
            e.getStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }
    
	/**
	 * @author 张琪
	 * @Description 根据主键ID获取药剂信息
	 * @return 药剂信息信息
	 */
	@ApiOperation(value = "根据主键编码获取药剂信息", notes = "参数：主键ID")
	@GetMapping(value = "/medicineinfoInfo/{ypId}")
	public ResponseWrapper getMedicineinfoInfoByLsId(@PathVariable("ypId")String ypId) {
		try {
			DYpxxVO medicineinfoVO=dYpxxService.getDYpxxInfoByLsId(ypId);
			if(medicineinfoVO == null) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "获取失败", null);
			}
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), medicineinfoVO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
	}
	
    
	/**
	 * 药剂信息信息-更改备案状态
	 * @param dYpxxHisVO
	 * @return
	 */
	@PostMapping(value = "/medicineinfoStatus")
	@ApiOperation(value = "药剂信息信息-更改备案状态", notes = "参数：备案信息")
	public ResponseWrapper updateMedicineinfoStatus(@RequestBody DYpxxHisVO dYpxxHisVO) {
		try {
			if (dYpxxHisVO.getSfba() == null) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
			Integer i = dYpxxService.updateMedicineinfoStatus(dYpxxHisVO);
			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(), "服务错误，请联系管理员");
		}
	}

	@ApiOperation(value = "查询报表list，分页", notes = "参数：企业库点名称，时间模糊查询、当前页、页面大小")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "page", value = "起始页", required = true),
			@ApiImplicitParam(name = "size", value = "每页展示行数", required = true)})
	@PostMapping(value = "/reportsInfo")
	public ResponseWrapper findReportsInfoList(@RequestBody KdBaseInfoVO VO,
									  Integer page,
									  Integer size) throws Exception {
		Map<String, Object> result = dYpxxService.findReportsInfoList(VO, page, size);
		return ResponseWrapper.success(
				HttpStatus.OK.value(),
				HttpStatus.OK.getReasonPhrase(),
				result);
	}

	@ApiOperation(value = "根据库点id查询储量化学药剂保管台账信息", notes = "参数：储量化学药剂保管台账库点id")
	@GetMapping(value = "/clhxyjbgtz/{id}/")
	public ResponseWrapper findClhxyjbgtzByKddm(@PathVariable("id") String id) throws Exception {
		KdBaseInfoVO resVO = dYpxxService.findClhxyjbgtzByKddm(id);
		return ResponseWrapper.success(
				HttpStatus.OK.value(),
				HttpStatus.OK.getReasonPhrase(),
				resVO);
	}

	@ApiOperation(value = "根据库点id查询废弃危险化学品产生、贮存、利用处置情况表信息", notes = "参数：废弃危险化学品产生、贮存、利用处置情况表库点id")
	@GetMapping(value = "/fqwxhxpcszclyclqkb/{id}/")
	public ResponseWrapper findFqwxhxpcszclyclqkbByKddm(@PathVariable("id") String id) throws Exception {
		KdBaseInfoVO resVO = dYpxxService.findFqwxhxpcszclyclqkbByKddm(id);
		return ResponseWrapper.success(
				HttpStatus.OK.value(),
				HttpStatus.OK.getReasonPhrase(),
				resVO);
	}

	@ApiOperation(value = "根据库点id查询危险化学品环境管理排查基本表信息", notes = "参数：危险化学品环境管理排查基本表信息库点id")
	@GetMapping(value = "/wxhxphjglpcjbbByKdmc/{id}/")
	public ResponseWrapper findWxhxphjglpcjbbVOBykdmc(@PathVariable("id") String id) throws Exception {
		KdBaseInfoVO resVO = dYpxxService.findWxhxphjglpcjbbByKdmc(id);
		return ResponseWrapper.success(
				HttpStatus.OK.value(),
				HttpStatus.OK.getReasonPhrase(),
				resVO);
	}

	@ApiOperation(value = "根据库点代码和时间查询危险化学品环境管理排查基本表信息", notes = "参数：危险化学品环境管理排查基本表信息库点代码和时间")
	@PostMapping(value = "/wxhxphjglpcjbbApp/")
	public ResponseWrapper findWxhxphjglpcjbbVOApp(@RequestBody KdBaseInfoVO VO) throws Exception {
		List<WxhxphjglpcjbbVO> resVO = dYpxxService.findWxhxphjglpcjbbApp(VO);
		return ResponseWrapper.success(
				HttpStatus.OK.value(),
				HttpStatus.OK.getReasonPhrase(),
				resVO);
	}
}
