package com.bweb.controller;

import com.bweb.controller.goods.GoodsController;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.security.SessionObject;
import com.hfkj.common.security.UserCenter;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.BsProductConfig;
import com.hfkj.model.ResponseData;
import com.hfkj.model.SecUserSessionObject;
import com.hfkj.service.BsProductConfigService;
import com.hfkj.sysenum.order.OrderChildProductTypeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;


@Controller
@RequestMapping(value="/productConfig")
@Api(value="商户产品折扣配置")
public class BsProductConfigController {

    private static final Logger log = LoggerFactory.getLogger(GoodsController.class);

    @Resource
    private UserCenter userCenter;

    @Resource
    private BsProductConfigService bsProductConfigService;


    @RequestMapping(value="/editProductConfig",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "编辑产品配置")
    public ResponseData editProductConfig(@RequestBody BsProductConfig body, HttpServletRequest request) {
        try {

            SessionObject sessionObject = userCenter.getSessionObject(request);
            SecUserSessionObject userModel = (SecUserSessionObject) sessionObject.getObject();

            if (body == null
                    || body.getMerId() == null
                    || body.getProductType() == null
            ) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            BsProductConfig productConfig = bsProductConfigService.queryDetail(body.getProductType());

            if (productConfig != null && !productConfig.getMerId().equals(body.getMerId())) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "当前产品已有商户配置，不可配置！");
            }

            if (productConfig != null) {
                body.setId(productConfig.getId());
            }

            BsProductConfig bsProductConfig;

            if (body.getId() != null) {
                // 查询
                bsProductConfig = bsProductConfigService.queryDetail(body.getId());
                if (bsProductConfig == null) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.CONTENT_NOT_FOUND, "");
                }
            } else {
                bsProductConfig = new BsProductConfig();
                bsProductConfig.setCreateTime(new Date());

            }
            bsProductConfig.setProductType(body.getProductType());
            bsProductConfig.setDiscount(body.getDiscount());
            bsProductConfig.setMerId(body.getMerId());
            bsProductConfig.setUpdateTime(new Date());
            bsProductConfig.setStatus(1);

            OrderChildProductTypeEnum typeEnum = OrderChildProductTypeEnum.getData(body.getProductType());
            if (typeEnum == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "当前产品类型未配置！");
            }

            bsProductConfig.setProductName(typeEnum.getName());
            bsProductConfig.setOpId(userModel.getAccount().getId());
            bsProductConfig.setOpName(userModel.getAccount().getUserName());

            if (body.getId() != null) {
                bsProductConfigService.update(bsProductConfig);
            } else {
                bsProductConfigService.create(bsProductConfig);
            }

            return ResponseMsgUtil.success("操作成功");

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

    }

    @RequestMapping(value="/getList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询列表")
    public ResponseData getList(@RequestParam(value = "merId" , required = false) Long merId) {
        try {

            return ResponseMsgUtil.success(bsProductConfigService.getList(merId));

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

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


            bsProductConfigService.delete(id , false);

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

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

}
