package com.bweb.controller;

import com.alibaba.fastjson.JSONObject;
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.BsGasOilGunNo;
import com.hfkj.entity.BsGasOilPrice;
import com.hfkj.entity.BsMerchant;
import com.hfkj.model.ResponseData;
import com.hfkj.model.SecUserSessionObject;
import com.hfkj.service.gas.BsGasOilGunNoService;
import com.hfkj.service.gas.BsGasOilPriceService;
import com.hfkj.service.merchant.BsMerchantService;
import com.hfkj.service.gas.BsGasService;
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.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping(value = "/gasOilGunNo")
@Api(value = "加油站价格")
public class BsGasOilGunNoController {
    private static Logger log = LoggerFactory.getLogger(BsGasOilGunNoController.class);

    @Resource
    private BsGasOilPriceService gasOilPriceService;
    @Resource
    private BsGasOilGunNoService gasOilGunNoService;
    @Resource
    private BsMerchantService merchantService;
    @Resource
    private BsGasService gasService;
    @Resource
    private UserCenter userCenter;

    @RequestMapping(value = "/createOilGunNo", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "创建油品枪号")
    public ResponseData createOilGunNo(@RequestBody BsGasOilGunNo body) {
        try {
            SecUserSessionObject userInfoModel = userCenter.getSessionModel(SecUserSessionObject.class);
            if (userInfoModel == null) {
                log.error("BsGasOilGunNoController --> createOilGunNo() error!", "用户身份错误或已过期");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.ACCOUNT_LOGIN_NOT, "");
            }
            if (body == null
                    || body.getMerNo() == null
                    || StringUtils.isBlank(body.getOilNo())
                    || StringUtils.isBlank(body.getGunNo())){
                log.error("BsGasOilGunNoController --> createOilGunNo() error!", "参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            // 查询商户
            BsMerchant merchant = merchantService.getMerchant(body.getMerNo());
            if (merchant == null) {
                log.error("BsGasOilGunNoController --> createOilGunNo() error!", "未知的商户");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的商户");
            }
            // 油品
            BsGasOilPrice oilPrice = gasOilPriceService.getGasOilPrice(merchant.getId(), body.getOilNo());
            if (oilPrice == null) {
                log.error("BsGasOilGunNoController --> createOilGunNo() error!", "商户未添加" + body.getOilNo() + "油品");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "商户未添加" + body.getOilNo() + "油品");
            }
            if (gasOilGunNoService.getDetail(body.getMerNo(), body.getOilNo(), body.getGunNo()) != null) {
                log.error("BsGasOilGunNoController --> createOilGunNo() error!", "油品枪号已存在");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "油品枪号已存在");
            }

            BsGasOilGunNo oilGunNo = new BsGasOilGunNo();
            oilGunNo.setGasOilPriceId(oilPrice.getId());
            oilGunNo.setMerId(oilPrice.getMerId());
            oilGunNo.setMerNo(oilPrice.getMerNo());
            oilGunNo.setOilType(oilPrice.getOilType());
            oilGunNo.setOilTypeName(oilPrice.getOilTypeName());
            oilGunNo.setOilNo(oilPrice.getOilNo());
            oilGunNo.setOilNoName(oilPrice.getOilNoName());
            oilGunNo.setGunNo(body.getGunNo());
            oilGunNo.setStatus(1);
            gasOilGunNoService.editData(oilGunNo);

            // 删除油站缓存
            gasService.clean(oilPrice.getMerNo());
            return ResponseMsgUtil.success("操作成功");

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

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "删除抢号")
    public ResponseData delete(@RequestBody JSONObject body) {
        try {
            if (body == null || body.getLong("gunNoId") == null) {
                log.error("BsGasOilGunNoController --> delete() error!", "请求参数校验失败");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            gasOilGunNoService.delete(body.getLong("gunNoId"));


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

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

    @RequestMapping(value = "/queryOilDetail", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询油品详情")
    public ResponseData queryOilDetail(@RequestParam(value = "gunNoId", required = true) Long gunNoId) {
        try {

          return ResponseMsgUtil.success(gasOilGunNoService.getDetail(gunNoId));

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

    @RequestMapping(value = "/queryGunNoList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询油品列表")
    public ResponseData queryGunNoList(@RequestParam(value = "merNo", required = true) String merNo,
                                       @RequestParam(value = "oilNo", required = false) String oilNo) {
        try {
            // 查询枪号
            List<BsGasOilGunNo> list = gasOilGunNoService.getOilGunNoList(merNo);

            if (StringUtils.isNotBlank(oilNo)) {
                return ResponseMsgUtil.success(list.stream().filter(o -> o.getOilNo().equals(oilNo)).collect(Collectors.toList()));
            }

            return ResponseMsgUtil.success(list);

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


}
