package com.bweb.controller.goods;

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.GoodsType;
import com.hfkj.model.goods.GoodsTypeModel;
import com.hfkj.model.ResponseData;
import com.hfkj.model.SecUserSessionObject;
import com.hfkj.service.goods.GoodsTypeService;
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.*;

@Controller
@RequestMapping(value="/goodsType")
@Api(value="商品类型")
public class GoodsTypeController {
    private static final Logger log = LoggerFactory.getLogger(GoodsTypeController.class);

    @Resource
    private GoodsTypeService goodsTypeService;

    @Resource
    private UserCenter userCenter;

    @RequestMapping(value="/editGoodsType",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "编辑商品类型")
    public ResponseData editGoodsType(@RequestBody GoodsType body, HttpServletRequest request) {

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

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

        GoodsType goodsType;

        if (body.getId() != null) {
            // 查询商品类型
            goodsType = goodsTypeService.queryDetail(body.getId());
            if (goodsType == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
        } else {
            goodsType = new GoodsType();
            goodsType.setCreateTime(new Date());
        }

        goodsType.setUpdateTime(new Date());
        goodsType.setStatus(1);
        goodsType.setTitle(body.getTitle());
        goodsType.setParentId(body.getParentId());
        goodsType.setSort(body.getSort());
        goodsType.setBusinessType(body.getBusinessType());
        goodsType.setImgUrl(body.getImgUrl());
        goodsType.setOpId(userModel.getAccount().getId());
        goodsType.setOpName(userModel.getAccount().getUserName());

        if (body.getId() != null) {
            goodsTypeService.update(goodsType);
        } else {
            goodsTypeService.create(goodsType);
        }

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

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


            List<GoodsType> list = goodsTypeService.getList(new HashMap<>());

            List<GoodsTypeModel> goodsTypeModels = goodsTypeService.getGoodsTypeModelList(list);


            return ResponseMsgUtil.success(goodsTypeModels);

        } 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 {

            Map<String , Object> map = new HashMap<>();
            map.put("parentId" , id);
            List<GoodsType> list = goodsTypeService.getList(map);

            if (!list.isEmpty()) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "存在下级分类，不可删除！");
            }

            goodsTypeService.delete(id , false);

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

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

}
