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.HighGoodsType;
import com.hai.model.HighAgentModel;
import com.hai.model.ResponseData;
import com.hai.model.UserInfoModel;
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 = "/highGoodsType")
@Api(value = "商品类型接口")
public class HighGoodsTypeController {

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

    @Autowired
    private UserCenter userCenter;

    @Resource
    private HighGoodsTypeService highGoodsTypeService;

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

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

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

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

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

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

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

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

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

            HighGoodsType highGoodsTypes = highGoodsTypeService.findById(highGoodsType.getId());

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

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

            highGoodsTypeService.updateGoodsType(highGoodsType);

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

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


    @RequestMapping(value = "/insertGoodsType", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增商品类型")
    public ResponseData insertGoodsType(@RequestBody HighGoodsType highGoodsType, HttpServletRequest request) {
        try {
            //发布人员
            SessionObject sessionObject = userCenter.getSessionObject(request);
            UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();


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

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

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

            highGoodsTypeService.insertGoodsType(highGoodsType);

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

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

    @RequestMapping(value = "/updateGoodsType", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改商品类型")
    public ResponseData updateGoodsType(@RequestBody HighGoodsType highGoodsType, HttpServletRequest request) {
        try {
            //发布人员
            SessionObject sessionObject = userCenter.getSessionObject(request);
            UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();


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

            HighGoodsType highGoodsTypes = highGoodsTypeService.findById(highGoodsType.getId());

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

            highGoodsTypes.setUpdatedTime(new Date());
            highGoodsTypes.setUpdatedUserId(userInfoModel.getSecUser().getId().intValue());
            highGoodsTypes.setImg(highGoodsType.getImg());
            highGoodsTypes.setTitle(highGoodsType.getTitle());

            highGoodsTypeService.updateGoodsType(highGoodsTypes);

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

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