package com.org.hktshop.controller.product;

import com.org.hktshop.Interface.product.IHktCountRegulationService;
import com.org.hktshop.base.BaseResult;
import com.org.hktshop.controller.BaseController;
import com.org.hktshop.entity.product.HktCountRegulation;
import com.org.hktshop.utils.HttpCode;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 数量规程
 * @author owen
 */
@RestController
@RequestMapping("/async/count")
public class HktCountRegulationController extends BaseController {

    @Autowired
    private IHktCountRegulationService hktCountRegulationService;
    /****
     * 新增/修改 数量规则
     * @param hktCountRegulation
     * @return
     */
    @RequestMapping(value = "/updateCountRegulation",method = RequestMethod.POST)
    @ApiOperation(value = "新增/修改 数量规则")
    public Object updateCountTemplate(@RequestBody HktCountRegulation hktCountRegulation) {
        try {
//            if(hktCountRegulation.getType() > 5){
//                hktCountRegulation.setParentId(0+"");
//                hktCountRegulation.setCategoryId(0+"");
//            }
            Object obj = hktCountRegulationService.updateCountTemplate(hktCountRegulation);
            return obj;
        } catch (Exception ex) {
            logger.info("新增/修改数量规则错误  error = {}",ex.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), HttpCode.INTERNAL_SERVER_ERROR.getMsg());
        }
    }

    /****
     * 删除价格规则
     * @param hktCountRegulation
     * @return
     */
    @RequestMapping(value = "/delPriceRegulation",method = RequestMethod.POST)
    @ApiOperation(value = "删除数量规则")
    public Object delCountTemplate(@RequestBody HktCountRegulation hktCountRegulation) {
        try {
            hktCountRegulationService.delCountTemplate(hktCountRegulation);
            return BaseResult.success();
        } catch (Exception ex) {
            logger.info("删除数量规则错误  error = {}",ex.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), HttpCode.INTERNAL_SERVER_ERROR.getMsg());
        }
    }

    @ApiOperation(value = "分页查询数量规则", notes = "分页查询数量规则", httpMethod = "POST")
    @RequestMapping(value = "/queryList",method = RequestMethod.POST)
    public Object queryCountRegulationByPage(@RequestBody HktCountRegulation hktCountRegulation) {
        try {
            Object page = hktCountRegulationService.queryCountRegulationByPage(hktCountRegulation);
            return BaseResult.successWithData(page);
        } catch (Exception e) {
            logger.info("分页查询数量规则列表错误  error = {}",e.toString());
            e.printStackTrace();
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }

    @ApiOperation(value = "数量规则详情", notes = "数量规则详情", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "数量规则主键")
    })
    @RequestMapping("/findCountRegulation")
    public Object findCountRegulation(@RequestBody HktCountRegulation hktCountRegulation) {
        try {
            HktCountRegulation hktThe = hktCountRegulationService.findCountRegulation(hktCountRegulation);
            return BaseResult.successWithData(hktThe);
        } catch (Exception e) {
            logger.info("价格数量查询错误  error = {}",e.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }

    @ApiOperation(value = "批量修改数量规则", notes = "批量修改数量规则", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "需要修改的规则主键id")
    })
    @RequestMapping("/batchCountRegulation")
    public Object batchCountRegulation(@RequestBody HktCountRegulation hktCountRegulation) {
        try {
            hktCountRegulationService.batchCountRegulation(hktCountRegulation);
            return BaseResult.success();
        } catch (Exception e) {
            logger.info("批量修改数量规则错误  error = {}",e.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }

    @ApiOperation(value = "同步数据库到缓存", notes = "同步数据库到缓存", httpMethod = "POST")
    @RequestMapping("/synCountRegulation")
    public Object synCountRegulation() {
        try {
            hktCountRegulationService.synCountRegulation();
            return BaseResult.success();
        } catch (Exception e) {
            logger.info("同步数据库到缓存  error = {}",e.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }
}
