package com.bweb.controller.goods;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.security.SessionObject;
import com.hfkj.common.security.UserCenter;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.GoodsBrand;
import com.hfkj.entity.GoodsType;
import com.hfkj.model.ResponseData;
import com.hfkj.model.SecUserSessionObject;
import com.hfkj.service.goods.GoodsBrandService;
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.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;

@Controller
@RequestMapping(value="/goodsBrand")
@Api(value="商品品牌")
public class GoodsBrandController {
    private static final Logger log = LoggerFactory.getLogger(GoodsBrandController.class);

    @Resource
    private GoodsBrandService goodsBrandService;

    @Resource
    private UserCenter userCenter;

    @RequestMapping(value="/editGoodsBrand",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "编辑商品品牌")
    public ResponseData editGoodsBrand(@RequestBody GoodsBrand body, HttpServletRequest request) {
        try {

            SessionObject sessionObject = userCenter.getSessionObject(request);
            SecUserSessionObject userModel = (SecUserSessionObject) sessionObject.getObject();

            if (body == null
                    || StringUtils.isBlank(body.getTitle())) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            GoodsBrand goodsBrand;

            if (body.getId() != null) {
                // 查询品牌
                goodsBrand = goodsBrandService.queryDetail(body.getId());
                if (goodsBrand == null) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.CONTENT_NOT_FOUND, "");
                }
            } else {
                goodsBrand = new GoodsBrand();
                goodsBrand.setCreateTime(new Date());
            }

            goodsBrand.setUpdateTime(new Date());
            goodsBrand.setStatus(1);
            goodsBrand.setTitle(body.getTitle());
            goodsBrand.setImgUrl(body.getImgUrl());
            goodsBrand.setOpId(userModel.getAccount().getId());
            goodsBrand.setOpName(userModel.getAccount().getUserName());

            if (body.getId() != null) {
                goodsBrandService.update(goodsBrand);
            } else {
                goodsBrandService.create(goodsBrand);
            }

            return ResponseMsgUtil.success("操作成功");

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

    }

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

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

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

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

    @RequestMapping(value="/getListBrandAll",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询列表")
    public ResponseData getListBrandAll() {
        try {

            return ResponseMsgUtil.success(goodsBrandService.getList(new HashMap<>()));

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

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


            goodsBrandService.delete(id , false);

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

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


}
