package com.bweb.controller;

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.*;
import com.hai.entity.ApiMemberProduct;
import com.hai.entity.ApiMerchants;
import com.hai.model.ResponseData;
import com.hai.model.UserInfoModel;
import com.hai.service.ApiMemberProductService;
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.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/apiMemberProduct")
@Api(value = "api商户")
public class ApiMemberProductController {

    Logger log = LoggerFactory.getLogger(ApiMemberProductController.class);

    @Resource
    private ApiMemberProductService apiMemberProductService;

    @Autowired
    private UserCenter userCenter;

    @Resource
    private RedisUtil redisUtil;


    @RequestMapping(value = "/getListApiMemberProduct", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询产品列表")
    public ResponseData getListApiMemberProduct(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "typeId", required = false) Integer typeId,
            @RequestParam(value = "brandId", required = false) String brandId,
            @RequestParam(name = "pageNum", required = true) Integer pageNum,
            @RequestParam(name = "pageSize", required = true) Integer pageSize
    ) {
        try {

            Map<String, Object> map = new HashMap<>();

            map.put("name", name);
            map.put("typeId", typeId);
            map.put("brandId", brandId);

            PageHelper.startPage(pageNum,pageSize);

            List<ApiMemberProduct> list = apiMemberProductService.getListApiMemberProduct(map);
            return ResponseMsgUtil.success(new PageInfo<>(list));

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


    @RequestMapping(value="/insertMemberProduct",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增产品")
    public ResponseData insertMemberProduct(@RequestBody ApiMemberProduct apiMemberProduct, HttpServletRequest request) {
        try {
            SessionObject sessionObject = userCenter.getSessionObject(request);
            UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();
            if (userInfoModel.getBsCompany() == null) {
                log.error("HighMerchantController -> updateMerchant() error!","该主角色没有权限");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.MENU_TREE_HAS_NOT_ERROR, "");
            }

            if (StringUtils.isBlank(apiMemberProduct.getName()) ||
                apiMemberProduct.getTypeId() == null ||
                    StringUtils.isBlank(apiMemberProduct.getProductId()) ||
                    apiMemberProduct.getBrandId() == null ||
                    apiMemberProduct.getCostPrice() == null ||
                    apiMemberProduct.getPrice() == null ) {
                log.error("ApiMemberProductController -> insertMemberProduct() error!","参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            apiMemberProduct.setCreateTime(new Date());
            apiMemberProduct.setUpdateTime(new Date());
            apiMemberProduct.setOperatorId(userInfoModel.getSecUser().getId());
            apiMemberProduct.setOperatorName(userInfoModel.getSecUser().getUserName());
            apiMemberProduct.setStatus(102);
            apiMemberProductService.insertApiMemberProduct(apiMemberProduct);

            return ResponseMsgUtil.success("新增成功");
        } catch (Exception e) {
            log.error("ApiMerchantsController -> insertMerchant() error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/updateMemberProduct",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改产品")
    public ResponseData updateMemberProduct(@RequestBody ApiMemberProduct apiMemberProduct, HttpServletRequest request) {
        try {
            SessionObject sessionObject = userCenter.getSessionObject(request);
            UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();
            if (userInfoModel.getBsCompany() == null) {
                log.error("HighMerchantController -> updateMerchant() error!","该主角色没有权限");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.MENU_TREE_HAS_NOT_ERROR, "");
            }

            if (StringUtils.isBlank(apiMemberProduct.getName()) ||
                    apiMemberProduct.getId() == null ||
                    apiMemberProduct.getTypeId() == null ||
                    StringUtils.isBlank(apiMemberProduct.getProductId()) ||
                    apiMemberProduct.getBrandId() == null ||
                    apiMemberProduct.getCostPrice() == null ||
                    apiMemberProduct.getPrice() == null ) {
                log.error("ApiMemberProductController -> insertMemberProduct() error!","参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }


            apiMemberProduct.setUpdateTime(new Date());
            apiMemberProduct.setOperatorId(userInfoModel.getSecUser().getId());
            apiMemberProduct.setOperatorName(userInfoModel.getSecUser().getUserName());
            apiMemberProduct.setStatus(102);
            apiMemberProductService.updateApiMemberProduct(apiMemberProduct);

            return ResponseMsgUtil.success("修改成功");
        } catch (Exception e) {
            log.error("ApiMerchantsController -> insertMerchant() error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

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

            return ResponseMsgUtil.success(apiMemberProductService.findById(id));

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


}
