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.ResponseMsgUtil;
import com.hai.entity.HighBrand;
import com.hai.entity.HighGoodsType;
import com.hai.model.ResponseData;
import com.hai.model.UserInfoModel;
import com.hai.service.HighBrandService;
import com.hai.service.HighGoodsTypeService;
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.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: 袁野
 * @Description:
 * @Date: 2021/3/26 23:08
 */
@Controller
@RequestMapping(value = "/highBrand")
@Api(value = "品牌接口")
public class HighBrandController {

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

    @Autowired
    private UserCenter userCenter;

    @Resource
    private HighGoodsTypeService highGoodsTypeService;

    @Resource
    private HighBrandService highBrandService;

    @RequestMapping(value = "/getBrandByList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取品牌列表")
    public ResponseData getBrandByList(@RequestParam(name = "pageNum", required = true) Integer pageNum,
                                       @RequestParam(name = "pageSize", required = true) Integer pageSize,
                                       @RequestParam(name = "title", required = false) String title,
                                       @RequestParam(name = "goodTypeId", required = false) String goodTypeId,
                                       HttpServletRequest request) {
        try {

            Map<String,String> map = new HashMap<>();
            map.put("title", title);
            map.put("goodTypeId", goodTypeId);

            PageHelper.startPage(pageNum,pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(highBrandService.getListBand(map)));

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

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

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

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

    @RequestMapping(value = "/getByDelete", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "根据id删除")
    public ResponseData getByDelete(
            @RequestBody HighBrand highBrand,
            HttpServletRequest request
    ) {
        try {

            //发布人员
            SessionObject sessionObject = userCenter.getSessionObject(request);
            UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();

            HighBrand highBrands = highBrandService.findById(highBrand.getId());

            if (highBrands == null) {
                log.error("HighGoodsTypeController --> getGoodsTypeByDelete() error!", "当前品牌不存在");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.DELETE_DATA_ERROR, "");
            }

            highBrands.setStatus(0);
            highBrands.setUpdatedUserId(userInfoModel.getSecUser().getId().intValue());
            highBrands.setUpdatedTime(new Date());

            highBrandService.updateBrand(highBrands);

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

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


    @RequestMapping(value = "/insertBrand", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增品牌")
    public ResponseData insertBrand(@RequestBody HighBrand highBrand, HttpServletRequest request) {
        try {
            //发布人员
            SessionObject sessionObject = userCenter.getSessionObject(request);
            UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();


            if (StringUtils.isBlank(highBrand.getTitle())
                    || StringUtils.isBlank(highBrand.getImg())
                    || highBrand.getGoodTypeId() == null
            ) {
                log.error("HighAgentController -> insertAgent() error!","参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            // 校验账号用户名是否存在
            if (highBrandService.findByTitle(highBrand.getTitle() , highBrand.getGoodTypeId())) {
                log.error("HighGoodsTypeController --> insertGoodsType() error!", "已存在");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.ADD_REPEATEDLY, "");
            }

            highBrand.setCreatedTime(new Date());
            highBrand.setCreatedUserId(userInfoModel.getSecUser().getId().intValue());
            highBrand.setUpdatedTime(new Date());
            highBrand.setUpdatedUserId(userInfoModel.getSecUser().getId().intValue());
            highBrand.setStatus(1);

            highBrandService.insertBrand(highBrand);

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

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

    @RequestMapping(value = "/updateBrand", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改品牌")
    public ResponseData updateBrand(@RequestBody HighBrand highBrand, HttpServletRequest request) {
        try {
            //发布人员
            SessionObject sessionObject = userCenter.getSessionObject(request);
            UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();


            if (StringUtils.isBlank(highBrand.getTitle())
                    || StringUtils.isBlank(highBrand.getImg())
                    || highBrand.getId() == null
                    || highBrand.getGoodTypeId() == null
            ) {
                log.error("HighAgentController -> insertAgent() error!","参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            HighBrand highBrands = highBrandService.findById(highBrand.getId());

            // 校验账号用户名是否存在
            if (highBrandService.findByTitle(highBrand.getTitle() , highBrand.getGoodTypeId()) && !highBrand.getId().equals(highBrands.getId())) {
                log.error("HighGoodsTypeController --> insertGoodsType() error!", "已存在");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.ADD_REPEATEDLY, "");
            }

            highBrands.setUpdatedTime(new Date());
            highBrands.setUpdatedUserId(userInfoModel.getSecUser().getId().intValue());
            highBrands.setImg(highBrand.getImg());
            highBrands.setTitle(highBrand.getTitle());
            highBrands.setGoodTypeId(highBrand.getGoodTypeId());

            highBrandService.updateBrand(highBrands);

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

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