package com.bweb.controller.Goods;

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.GoodsDetail;
import com.hai.entity.GoodsRegionFreight;
import com.hai.entity.HighGoodsType;
import com.hai.entity.SecRegion;
import com.hai.goods.service.GoodsRegionFreightService;
import com.hai.model.ResponseData;
import com.hai.model.UserInfoModel;
import com.hai.service.CommonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.*;

@Controller
@RequestMapping(value = "/regionFreight")
@Api(value = "区域运费业务")
public class GoodsRegionFreightController {

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

    @Resource
    private GoodsRegionFreightService regionFreightService;

    @Autowired
    private UserCenter userCenter;

    @Resource
    private CommonService commonService;

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


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

            map.put("regionName", regionName);
            map.put("regionId", regionId);

            PageHelper.startPage(pageNum,pageSize);

            List<GoodsRegionFreight> list = regionFreightService.getRegionFreightList(map);
            return ResponseMsgUtil.success(new PageInfo<>(list));

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

    @RequestMapping(value = "/insertRegionFreight", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增产品")
    public ResponseData insertRegionFreight(@RequestBody GoodsRegionFreight regionFreight, HttpServletRequest request) {
        try {

            SessionObject sessionObject = userCenter.getSessionObject(request);
            UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();

            if (regionFreight == null ||
                    regionFreight.getRegionId() == null ||
                    regionFreight.getFreightPrice() == null ||
                    regionFreight.getFreePostPrice() == null
            ) {
                log.error("GoodsDetailController -> insertProduct() error!");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            GoodsRegionFreight goodsRegionFreight = regionFreightService.findRegionFreightByRegionId(regionFreight.getRegionId());

            if (goodsRegionFreight != null) {
                log.error("GoodsDetailController -> insertProduct() error!");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "当前区域已经配置！");
            }

            SecRegion region = commonService.getRegionsById(Long.valueOf(regionFreight.getRegionId()));

            if (region == null) {
                log.error("GoodsDetailController -> insertProduct() error!");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "区域错误！");
            }

            regionFreight.setRegionName(region.getRegionName());
            regionFreight.setOpId(userInfoModel.getSecUser().getId());
            regionFreight.setStatus(1);
            regionFreight.setOpName(userInfoModel.getSecUser().getLoginName());
            regionFreight.setCreateTime(new Date());
            regionFreight.setUpdateTime(new Date());

            regionFreightService.insertRegionFreight(regionFreight);

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

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

    @RequestMapping(value = "/updateRegionFreight", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改产品")
    public ResponseData updateRegionFreight(@RequestBody GoodsRegionFreight regionFreight, HttpServletRequest request) {
        try {

            SessionObject sessionObject = userCenter.getSessionObject(request);
            UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();

            if (regionFreight == null ||
                    regionFreight.getId() == null ||
                    regionFreight.getRegionId() == null ||
                    regionFreight.getFreightPrice() == null ||
                    regionFreight.getFreePostPrice() == null
            ) {
                log.error("GoodsDetailController -> insertProduct() error!");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            GoodsRegionFreight goodsRegionFreight = regionFreightService.findRegionFreightById(regionFreight.getId());

            GoodsRegionFreight freightByRegionId = regionFreightService.findRegionFreightByRegionId(regionFreight.getRegionId());



            if (goodsRegionFreight != null && !Objects.equals(freightByRegionId.getId(), goodsRegionFreight.getId())) {
                log.error("GoodsDetailController -> insertProduct() error!");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "当前区域已经配置！");

            }

            SecRegion region = commonService.getRegionsById(Long.valueOf(regionFreight.getRegionId()));

            if (region == null) {
                log.error("GoodsDetailController -> insertProduct() error!");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "区域错误！");
            }

            if (goodsRegionFreight == null) {
                log.error("GoodsDetailController -> insertProduct() error!");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到相关信息！");
            }

            regionFreight.setRegionName(region.getRegionName());
            regionFreight.setOpId(userInfoModel.getSecUser().getId());
            regionFreight.setStatus(goodsRegionFreight.getStatus());
            regionFreight.setOpName(userInfoModel.getSecUser().getLoginName());
            regionFreight.setCreateTime(goodsRegionFreight.getCreateTime());
            regionFreight.setUpdateTime(new Date());

            regionFreightService.updateRegionFreight(regionFreight);

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

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

    @RequestMapping(value = "/findRegionFreightById", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询商品")
    public ResponseData findRegionFreightById(
            @RequestParam(value = "id", required = true) Long id
    ) {
        try {

            GoodsRegionFreight goodsRegionFreight = regionFreightService.findRegionFreightById(id);


            if (goodsRegionFreight == null) {
                log.error("GoodsDetailController -> findGoodsDetailById() error!");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到相关信息");
            }

            return ResponseMsgUtil.success(goodsRegionFreight);

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

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

            GoodsRegionFreight goodsRegionFreight = regionFreightService.findRegionFreightById(id);


            if (goodsRegionFreight == null) {
                log.error("GoodsDetailController -> findGoodsDetailById() error!");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到相关信息");
            }

            regionFreightService.deleteRegionFreight(goodsRegionFreight.getId());

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

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