package com.unis.base.server.controller;

import com.github.pagehelper.PageInfo;
import com.unis.base.common.vo.PriceMonitorQueryVo;
import com.unis.base.common.vo.PriceMonitorVO;
import com.unis.base.server.service.PriceMonitorSerivce;
import com.unis.common.utils.ResponseWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhaoyaodng
 * @version 1.0
 * @Title TyJgjcxxbController
 * @ProjectName 价格监测信息明细 Controller
 * @Description
 * @date 2019/08/24 14：14
 */
@RestController
@Api(value = "价格监测信息明细 ", tags = "价格监测信息明细维护")
@Slf4j
public class PriceMonitorController {
    @Autowired
    private PriceMonitorSerivce priceMonitorSerivce;

    /**
     * @author zhengbodang
     * @Description 获取价格监测信息明细列表
     * @param
     * @return 价格监测信息明细列表
     */
    @ApiOperation(value = "价格监测信息明细列表", notes = "参数：")
    @PostMapping(value = "/pricemonitorList")
    public ResponseWrapper getpriceMonitorList(@RequestBody PriceMonitorQueryVo priceMonitorQueryVo){
        try {
            List<PriceMonitorVO> tyJgjcxxbV0s = priceMonitorSerivce.getPriceMonitorList(priceMonitorQueryVo);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), tyJgjcxxbV0s);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }

    /**
     * @author zhengbodang
     * @Description 分页获取价格监测信息明细列表
     * @param priceMonitorQueryVo,pageNum,pageSize
     * @return 分页查询信息
     */
    @ApiOperation(value = "分页获取计划信息明细列表", notes = "参数：")
    @PostMapping(value = "/pricemonitorPage/{pageNum}/{pageSize}")
    public ResponseWrapper getPriceMonitorPage(@RequestBody PriceMonitorQueryVo priceMonitorQueryVo , @PathVariable("pageNum")Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        try{
            PageInfo<PriceMonitorVO> pageInfos = new PageInfo<PriceMonitorVO>(priceMonitorSerivce.getPriceMonitorPage(priceMonitorQueryVo,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 zhaoyadong
     * @Description 新增价格监测信息明细信息
     * @param tyJgjcxxbVO models
     * @return 新增状态
     */
    @PostMapping(value = "/pricemonitor")
    @ApiOperation(value = "新增价格监测明细信息", notes = "参数：检测嗲名称，监测点代码")
    public ResponseWrapper insert(@RequestBody PriceMonitorVO tyJgjcxxbVO) {
        try{
        	if(StringUtils.isEmpty(tyJgjcxxbVO.getJcddm())||StringUtils.isEmpty(tyJgjcxxbVO.getJcdmc())) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
            int x = priceMonitorSerivce.insert(tyJgjcxxbVO);
            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(),"服务错误，请联系管理员");
    }

    /**
     * @author zhaoyadong
     * @Description 修改价格监测信息明细信息
     * @param tyJgjcxxbVO models
     * @return 修改状态
     */
    @PutMapping(value = "/pricemonitor")
    @ApiOperation(value = "修改价格监测明细信息", notes = "参数：检测信息id")
    public ResponseWrapper update(@RequestBody PriceMonitorVO tyJgjcxxbVO) {
        try{
        	if(StringUtils.isEmpty(tyJgjcxxbVO.getJgxxid())) {
				return ResponseWrapper.success(HttpStatus.BAD_REQUEST.value(), "有必传参数为空", null);
			}
            int x = priceMonitorSerivce.update(tyJgjcxxbVO);
            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(),"服务错误，请联系管理员");
    }
    /**
     * @author zhaoyadong
     * @Description 删除价格监测信息明细信息
     * @param
     * @return 状态
     */
    @DeleteMapping(value="/pricemonitor/{jgjcbm}")
    @ApiOperation(value = "根据价格监测编号删除价格监测信息明细", notes = "参数：")
    public ResponseWrapper delete(@PathVariable("jgjcbm") String PriceMonitorId) {
        try{
            int x = priceMonitorSerivce.delete(PriceMonitorId);
            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 zhaoyadong
     * @Description 根据编号价格监测获取信息
     * @param
     * @return 状态
     */
    @GetMapping(value="/pricemonitor/{jgjcbm}")
    @ApiOperation(value = "根据价格编号信息编号获取价格编号信息", notes = "参数：")
    public ResponseWrapper getTyCrkjcxxByTyjgjcid(@PathVariable("jgjcbm") String PriceMonitorId) {
        try{
            PriceMonitorVO tyJgjcxxbVO = priceMonitorSerivce.getPriceMonitorById(PriceMonitorId);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), tyJgjcxxbVO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }

    /**
     * @author zhaoyadong
     * @Description 根据编号价格监测获取信息
     * @param priceMonitorQueryVo models
     * @return
     */
    @PostMapping(value = "/LevelMonthlyReport")
    @ApiOperation(value = "根据年份查询该年分的月报", notes = "参数：priceMonitorQueryVo中的shsj为年份")
    public ResponseWrapper getTyCrkjcxxByTyjgjcid(@RequestBody PriceMonitorQueryVo priceMonitorQueryVo) {
        try{
            List<PriceMonitorQueryVo> levelMonthlyReport = priceMonitorSerivce.getLevelMonthlyReport(priceMonitorQueryVo);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), levelMonthlyReport);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }
}
