package com.bweb.controller;

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.utils.ResponseMsgUtil;
import com.hfkj.entity.BsMerchantChainBrand;
import com.hfkj.model.ResponseData;
import com.hfkj.service.merchant.BsMerchantChainBrandAccountService;
import com.hfkj.service.merchant.BsMerchantChainBrandService;
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.HashMap;
import java.util.Map;

/**
 * @className: BsMerchantChainBrandController
 * @author: HuRui
 * @date: 2024/8/14
 **/
@Controller
@RequestMapping(value = "/merchantChainBrand")
@Api(value = "商户连锁管理")
public class BsMerchantChainBrandController {
    private static Logger log = LoggerFactory.getLogger(BsMerchantChainBrandController.class);
    @Resource
    private BsMerchantChainBrandService merchantChainBrandService;
    @Resource
    private BsMerchantChainBrandAccountService merchantChainBrandAccountService;

    @RequestMapping(value = "/editChainBrand", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "编辑商户连锁")
    public ResponseData editChainBrand(@RequestBody BsMerchantChainBrand body) {
        try {
            if (body == null
                    || StringUtils.isBlank(body.getName())
                    || StringUtils.isBlank(body.getContactsName())
                    || StringUtils.isBlank(body.getContactsTelephone())
            ) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            BsMerchantChainBrand chainBrand;
            if (body.getId() != null) {
                // 查询商户连锁
                chainBrand = merchantChainBrandService.getDetail(body.getId());
                if (chainBrand == null) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到数据");
                }
            } else {
                chainBrand = new BsMerchantChainBrand();
                chainBrand.setStatus(1);
            }
            chainBrand.setName(body.getName());
            chainBrand.setContactsName(body.getContactsName());
            chainBrand.setContactsTelephone(body.getContactsTelephone());
            if (body.getId() != null) {
                merchantChainBrandService.update(chainBrand);
            } else {
                merchantChainBrandService.create(chainBrand);
            }
            return ResponseMsgUtil.success("操作成功");

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


    @RequestMapping(value = "/getDetail", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询")
    public ResponseData getDetail(@RequestParam(name = "id", required = true) Long id) {
        try {
            // 查询商户连锁
            BsMerchantChainBrand chainBrand = merchantChainBrandService.getDetail(id);
            if (chainBrand == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到数据");
            }
            Map<String,Object> param = new HashMap<>();
            param.put("chainBrand", chainBrand);
            param.put("account", merchantChainBrandAccountService.getAccount(chainBrand.getId()));
            return ResponseMsgUtil.success(param);

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


    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询商户连锁列表")
    public ResponseData getList(@RequestParam(name = "name", required = false) String name,
                                @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        try {
            Map<String,Object> param = new HashMap<>();
            param.put("name", name);

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

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

}
