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.GoodsPresent;
import com.hai.goods.service.GoodsPresentService;
import com.hai.model.HighUserModel;
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 = "/goodsPresent")
@Api(value = "商品赠送")
public class GoodsPresentController {

    Logger log = LoggerFactory.getLogger(GoodsPresentController.class);
    
    @Resource
    private GoodsPresentService presentService;

    @Autowired
    private UserCenter userCenter;

    @RequestMapping(value = "/getListPresent", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询列表")
    public ResponseData getListPresent(
            @RequestParam(value = "goodsId", required = false) Long goodsId
    ) {
        try {


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

            map.put("goodsId", goodsId);

            return ResponseMsgUtil.success(presentService.getPresentList(map));

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

    @RequestMapping(value = "/insertPresent", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增赠送内容")
    public ResponseData insertPresent(@RequestBody GoodsPresent present, HttpServletRequest request) {
        try {

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

            if (present == null ||
                    present.getGoodsId() == null ||
                    present.getNum() == null ||
                    present.getSourceId() == null ||
                    present.getSourceName() == null ||
                    present.getType() == null
            ) {
                log.error("GoodsDetailController -> insertProduct() error!");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

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

            presentService.insertPresent(present);

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

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

    @RequestMapping(value = "/updatePresent", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "更新赠送内容")
    public ResponseData updatePresent(@RequestBody GoodsPresent present, HttpServletRequest request) {
        try {

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

            if (present == null ||
                    present.getId() == null ||
                    present.getGoodsId() == null ||
                    present.getNum() == null ||
                    present.getSourceId() == null ||
                    present.getSourceName() == null ||
                    present.getType() == null
            ) {
                log.error("GoodsDetailController -> insertProduct() error!");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            GoodsPresent goodsPresent = presentService.findPresentById(present.getId());

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


            present.setOpId(userInfoModel.getSecUser().getId());
            present.setOpName(userInfoModel.getSecUser().getLoginName());
            present.setStatus(present.getStatus());
            present.setUpdateTime(new Date());

            presentService.insertPresent(present);

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

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

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

            GoodsPresent goodsPresent = presentService.findPresentById(id);

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


            goodsPresent.setStatus(String.valueOf(0));
            goodsPresent.setUpdateTime(new Date());
            goodsPresent.setOpId(userInfoModel.getSecUser().getId());
            goodsPresent.setOpName(userInfoModel.getSecUser().getLoginName());
            presentService.updatePresent(goodsPresent);

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

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

}
