package com.bweb.controller.group;

import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.security.UserCenter;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.BsStoreProduct;
import com.hfkj.service.group.BsStoreGroupService;
import com.hfkj.model.ResponseData;
import com.hfkj.model.UserInfoModel;
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.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@Api(value = "门店产品管理")
@RequestMapping(value = "/storeProduct")
public class BsStoreProductController {
    private static Logger log = LoggerFactory.getLogger(BsStoreProductController.class);

    @Resource
    private BsStoreGroupService storeGroupService;

    @Resource
    private UserCenter userCenter;

    @RequestMapping(value="/getStoreProductById",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询门店产品详情")
    public ResponseData getStoreProductById(@RequestParam(value = "id" , required = true) Long id) {
        try {
            return ResponseMsgUtil.success(storeGroupService.findStoreProduct(id));

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

    @RequestMapping(value="/getStoreProductByList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询门店产品列表")
    public ResponseData getStoreProductByList(@RequestParam(value = "storeId" , required = true) Long storeId) {
        try {
            Map<String , Object> map = new HashMap<>();
            map.put("storeId" , storeId);

            return ResponseMsgUtil.success(storeGroupService.getStoreProductByList(map));

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

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

            BsStoreProduct storeProduct = storeGroupService.findStoreProduct(id);
            storeProduct.setStatus(0);
            storeProduct.setUpdateTime(new Date());
            storeGroupService.editStoreProduct(storeProduct);

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

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

    @RequestMapping(value="/insertStoreProduct",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增门店产品")
    public ResponseData insertStoreProduct(@RequestBody BsStoreProduct storeProduct) {
        try {

            if (storeProduct == null
                    || storeProduct.getName() == null
                    || storeProduct.getStoreId() == null
                    || storeProduct.getPrice() == null
                    || storeProduct.getSpec() == null
                    || StringUtils.isBlank(storeProduct.getImg())

            ) {
                log.error("configStore error!","参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            UserInfoModel userInfoModel = userCenter.getSessionModel(UserInfoModel.class);
            if (userInfoModel == null || userInfoModel.getSalesman() == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "该角色暂无权限");
            }

            storeProduct.setStatus(1);
            storeProduct.setCreateTime(new Date());
            storeProduct.setUpdateTime(new Date());
            storeProduct.setOpId(userInfoModel.getSalesman().getId());
            storeProduct.setOpName(userInfoModel.getSalesman().getName());
            storeProduct.setSaleCount(0);

            storeGroupService.insertStoreProduct(storeProduct);


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

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

    @RequestMapping(value="/editStoreProduct",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "编辑门店产品")
    public ResponseData editStoreProduct(@RequestBody BsStoreProduct storeProduct) {
        try {

            if (storeProduct == null
                    || storeProduct.getId() == null
                    || storeProduct.getName() == null
                    || storeProduct.getStoreId() == null
                    || storeProduct.getPrice() == null
                    || storeProduct.getSpec() == null
                    || StringUtils.isBlank(storeProduct.getImg())

            ) {
                log.error("configStore error!","参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            UserInfoModel userInfoModel = userCenter.getSessionModel(UserInfoModel.class);
            if (userInfoModel == null || userInfoModel.getSalesman() == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "该角色暂无权限");
            }

            BsStoreProduct bsStoreProduct = storeGroupService.findStoreProduct(storeProduct.getId());

            if (bsStoreProduct == null) {
                throw ErrorHelp.genException(SysCode.System , ErrorCode.COMMON_ERROR , "暂无数据！");
            }

            bsStoreProduct.setUpdateTime(new Date());
            bsStoreProduct.setOpId(userInfoModel.getSalesman().getId());
            bsStoreProduct.setOpName(userInfoModel.getSalesman().getName());
            bsStoreProduct.setName(storeProduct.getName());
            bsStoreProduct.setSpec(storeProduct.getSpec());
            bsStoreProduct.setPrice(storeProduct.getPrice());
            bsStoreProduct.setImg(storeProduct.getImg());

            storeGroupService.editStoreProduct(bsStoreProduct);

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

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