package com.cweb.controller.group;

import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.security.UserCenter;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.BsStoreImg;
import com.hfkj.service.group.BsStoreGroupService;
import com.hfkj.model.ResponseData;
import com.hfkj.model.UserInfoModel;
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 java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@Api(value = "门店图片管理")
@RequestMapping(value = "/storeImg")
public class BsStoreImgController {
    private static Logger log = LoggerFactory.getLogger(BsStoreImgController.class);

    @Resource
    private BsStoreGroupService storeGroupService;

    @Resource
    private UserCenter userCenter;

    @RequestMapping(value="/getStoreImgById",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询门店图片详情")
    public ResponseData getStoreImgById(@RequestParam(value = "id" , required = true) Long id) {
        try {
            return ResponseMsgUtil.success(storeGroupService.findStoreImg(id));

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

    @RequestMapping(value="/getBsStoreImgByList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询门店图片列表")
    public ResponseData getBsStoreImgByList(@RequestParam(value = "storeId" , required = true) Long storeId,
                                        @RequestParam(value = "type" , required = false) Long type) {
        try {
            Map<String , Object> map = new HashMap<>();
            map.put("storeId" , storeId);
            map.put("type" , type);

            return ResponseMsgUtil.success(storeGroupService.getBsStoreImgByList(map));

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

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

            BsStoreImg bsStoreImg = storeGroupService.findStoreImg(id);
            bsStoreImg.setStatus(0);
            bsStoreImg.setUpdateTime(new Date());
            storeGroupService.editStoreImg(bsStoreImg);

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

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

    @RequestMapping(value="/insertStoreImg",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增门店图片")
    public ResponseData insertStoreImg(@RequestBody BsStoreImg storeImg) {
        try {

            if (storeImg == null
                    || storeImg.getType() == null
                    || storeImg.getStoreId() == null
                    || StringUtils.isBlank(storeImg.getImg())

            ) {
                log.error("configStore error!","参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            UserInfoModel userInfoModel = userCenter.getSessionModel(UserInfoModel.class);
            if (userInfoModel == null || userInfoModel.getSalesman() == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "该角色暂无权限");
            }
            String[] imgList = storeImg.getImg().split(",");
            for (String i : imgList) {
                storeImg.setStatus(1);
                storeImg.setCreateTime(new Date());
                storeImg.setUpdateTime(new Date());
                storeImg.setOpId(userInfoModel.getSalesman().getId());
                storeImg.setOpName(userInfoModel.getSalesman().getName());
                storeImg.setImg(i);
                storeGroupService.insertStoreImg(storeImg);
            }


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

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