package com.bweb.controller;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.common.security.SessionObject;
import com.hai.common.security.UserCenter;
import com.hai.common.utils.ResponseMsgUtil;
import com.hai.dao.BsIntegralRebateMapper;
import com.hai.entity.ApiKfcStores;
import com.hai.entity.BsIntegralRebate;
import com.hai.entity.BsOperationLog;
import com.hai.model.ResponseData;
import com.hai.model.UserInfoModel;
import com.hai.service.BsIntegralRebateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Controller
@RequestMapping(value = "/bsIntegralRebate")
@Api(value = "积分返利")
public class BsIntegralRebateController {
    private static Logger log = LoggerFactory.getLogger(BsIntegralRebateController.class);

    @Autowired
    private UserCenter userCenter;

    @Resource
    private BsIntegralRebateService bsIntegralRebateService;

    @RequestMapping(value = "/getIntegralRebateByList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取列表")
    public ResponseData getIntegralRebateByList(@RequestParam(name = "type", required = false) Integer type,
                                          @RequestParam(name = "productId", required = false) Long productId,
                                          @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                          @RequestParam(name = "pageSize", required = true) Integer pageSize, HttpServletRequest request) {
        try {

            SessionObject sessionObject = userCenter.getSessionObject(request);
            UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();

            Map<String, Object> map = new HashMap<>();
            map.put("type", type);
            map.put("companyId", userInfoModel.getBsCompany().getId());


            PageHelper.startPage(pageNum, pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(bsIntegralRebateService.getIntegralRebateByList(map)));
        } catch (Exception e) {
            log.error("BsCompanyController --> selectCompanyList() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/findIntegralRebateById", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据id查询详情")
    public ResponseData findIntegralRebateById(@RequestParam(name = "id", required = true) Long id) {
        try {

            return ResponseMsgUtil.success(bsIntegralRebateService.findIntegralRebateById(id));

        } catch (Exception e) {
            log.error("HighOrderController --> getOrderById() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/insertIntegralRebate", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增")
    public ResponseData insertIntegralRebate(@RequestBody BsIntegralRebate integralRebate, HttpServletRequest request) {
        try {

            SessionObject sessionObject = userCenter.getSessionObject(request);
            UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();

            if (integralRebate.getPercentage() == null ||
                    integralRebate.getType() == null ||
                    integralRebate.getStartTime() == null ||
                    integralRebate.getEndTime() == null ||
                    integralRebate.getProductId() == null
            ) {
                log.error("OutRechargePriceController -> insertPrice() error!");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            Map<String, Object> map = new HashMap<>();
            map.put("type" , integralRebate.getType());
            map.put("productId" , integralRebate.getProductId());
            map.put("companyId" , userInfoModel.getBsCompany().getId());

            BsIntegralRebate bsIntegralRebate = bsIntegralRebateService.findIntegralRebateByMap(map);

            if (bsIntegralRebate != null) {
                log.error("OutRechargePriceController -> insertPrice() error!");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "已存在当前产品");
            }

            integralRebate.setCompanyId(userInfoModel.getBsCompany().getId());
            integralRebate.setCreateTime(new Date());
            integralRebate.setOperatorName(userInfoModel.getSecUser().getUserName());
            integralRebate.setOperatorId(userInfoModel.getSecUser().getId());
            integralRebate.setUpdateTime(new Date());
            integralRebate.setStatus(101);

            bsIntegralRebateService.insertIntegralRebate(integralRebate);

            return ResponseMsgUtil.success("新增成功");

        } catch (Exception e) {
            log.error("OutRechargePriceController --> insertPrice() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/updateIntegralRebate", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改")
    public ResponseData updateIntegralRebate(@RequestBody BsIntegralRebate integralRebate, HttpServletRequest request) {
        try {

            SessionObject sessionObject = userCenter.getSessionObject(request);
            UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();

            if (integralRebate.getPercentage() == null ||
                    integralRebate.getId() == null ||
                    integralRebate.getStartTime() == null ||
                    integralRebate.getEndTime() == null ||
                    integralRebate.getType() == null ||
                    integralRebate.getProductId() == null
            ) {
                log.error("OutRechargePriceController -> insertPrice() error!");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            Map<String, Object> map = new HashMap<>();
            map.put("type" , integralRebate.getType());
            map.put("productId" , integralRebate.getProductId());

            BsIntegralRebate bsIntegralRebate = bsIntegralRebateService.findIntegralRebateByMap(map);

            if (bsIntegralRebate != null && !Objects.equals(integralRebate.getId(), bsIntegralRebate.getId())) {
                log.error("OutRechargePriceController -> insertPrice() error!");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "已存在当前产品");
            }

            integralRebate.setOperatorName(userInfoModel.getSecUser().getUserName());
            integralRebate.setOperatorId(userInfoModel.getSecUser().getId());
            integralRebate.setUpdateTime(new Date());
            integralRebate.setStatus(101);

            bsIntegralRebateService.updateIntegralRebate(integralRebate);

            return ResponseMsgUtil.success("修改成功");

        } catch (Exception e) {
            log.error("OutRechargePriceController --> insertPrice() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/deleteIntegralRebate", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "删除")
    public ResponseData deleteIntegralRebate(@RequestParam(value = "id", required = true) Long id) {
        try {

            bsIntegralRebateService.deleteIntegralRebate(id);
            return ResponseMsgUtil.success("删除成功");

        } catch (Exception e) {
            log.error("OutRechargePriceController --> deletePrice() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

}
