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.GoodsSku;
import com.hai.goods.service.GoodsDetailService;
import com.hai.goods.service.GoodsSkuService;
import com.hai.model.ResponseData;
import com.hai.model.UserInfoModel;
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.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/goodsSku")
@Api(value = "商品")
public class GoodsSkuController {

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

    @Resource
    private GoodsSkuService goodsSkuService;

    @Autowired
    private UserCenter userCenter;

    @RequestMapping(value = "/getListGoodsSku", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询商品SKU")
    public ResponseData getListGoodsSku(
            @RequestParam(value = "title", required = false) String title,
            @RequestParam(value = "goodsId", required = false) Long goodsId

    ) {
        try {


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

            map.put("title", title);
            map.put("goodsId", goodsId);

            return ResponseMsgUtil.success(goodsSkuService.getGoodsSkuList(map));

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

    @RequestMapping(value = "/insertGoodsSku", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增商品SKU")
    public ResponseData insertGoodsSku(@RequestBody GoodsSku goodsSku, HttpServletRequest request) {
        try {

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

            if (goodsSku == null ||
                    goodsSku.getName() == null ||
                    goodsSku.getGoodsId() == null ||
                    goodsSku.getShowImg() == null ||
                    goodsSku.getBannerImg() == null ||
                    goodsSku.getOriginalPrice() == null ||
                    goodsSku.getPrice() == null ||
                    goodsSku.getStock() == null

            ) {
                log.error("GoodsDetailController -> insertProduct() error!");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }


            goodsSku.setOpId(userInfoModel.getSecUser().getId());
            goodsSku.setStatus(1);
            goodsSku.setOpName(userInfoModel.getSecUser().getLoginName());
            goodsSku.setCreateTime(new Date());
            goodsSku.setUpdateTime(new Date());

            goodsSkuService.insertGoodsSku(goodsSku);

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

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

    @RequestMapping(value = "/updateGoodsSku", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "更新商品SKU")
    public ResponseData updateGoodsSku(@RequestBody GoodsSku goodsSku, HttpServletRequest request) {
        try {

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

            if (goodsSku == null ||
                    goodsSku.getId() == null ||
                    goodsSku.getName() == null ||
                    goodsSku.getGoodsId() == null ||
                    goodsSku.getShowImg() == null ||
                    goodsSku.getBannerImg() == null ||
                    goodsSku.getOriginalPrice() == null ||
                    goodsSku.getPrice() == null ||
                    goodsSku.getStock() == null

            ) {
                log.error("GoodsDetailController -> insertProduct() error!");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }


            GoodsSku sku = goodsSkuService.findGoodsSkuById(goodsSku.getId());

            goodsSku.setStatus(sku.getStatus());
            goodsSku.setCreateTime(sku.getCreateTime());
            goodsSku.setUpdateTime(new Date());

            goodsSku.setOpId(userInfoModel.getSecUser().getId());
            goodsSku.setOpName(userInfoModel.getSecUser().getLoginName());

            goodsSkuService.updateGoodsSku(goodsSku);


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

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

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

            GoodsSku goodsSku = goodsSkuService.findGoodsSkuById(id);

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

            return ResponseMsgUtil.success(goodsSku);

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

    @RequestMapping(value = "/deleteSku", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "删除SKU")
    public ResponseData deleteSku(
            @RequestParam(value = "id", required = true) Long id
            , HttpServletRequest request
    ) {
        try {
            SessionObject sessionObject = userCenter.getSessionObject(request);
            UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();

            GoodsSku goodsSku = goodsSkuService.findGoodsSkuById(id);

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

            goodsSku.setStatus(0);
            goodsSku.setUpdateTime(new Date());
            goodsSku.setOpId(userInfoModel.getSecUser().getId());
            goodsSku.setOpName(userInfoModel.getSecUser().getLoginName());
            goodsSkuService.updateGoodsSku(goodsSku);

            return ResponseMsgUtil.success(goodsSku);

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