package cn.iocoder.yudao.module.basic.service.businessinfo;

import cn.iocoder.yudao.framework.common.enums.CodeEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.basic.api.redis.RedisBasicInfoUtilImpl;
import cn.iocoder.yudao.module.basic.controller.admin.businessinfo.vo.BusinessInfoCreateReqVO;
import cn.iocoder.yudao.module.basic.controller.admin.businessinfo.vo.BusinessInfoPageReqVO;
import cn.iocoder.yudao.module.basic.controller.admin.businessinfo.vo.BusinessInfoUpdateReqVO;
import cn.iocoder.yudao.module.basic.convert.businessinfo.BusinessInfoConvert;
import cn.iocoder.yudao.module.basic.dal.dataobject.businessinfo.BusinessInfoDO;
import cn.iocoder.yudao.module.basic.dal.mysql.businessinfo.BusinessInfoMapper;
import cn.iocoder.yudao.module.basic.enums.ErrorCodeConstants;
import cn.iocoder.yudao.module.configure.api.uniTemplate.UniTemplateApi;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.module.basic.enums.ErrorCodeConstants.*;

/**
 * 商户 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class BusinessInfoServiceImpl implements BusinessInfoService {

    @Resource
    private BusinessInfoMapper businessInfoMapper;

    @Resource
    private UniTemplateApi uniTemplateApi;

    @Resource
    private RedisBasicInfoUtilImpl redisUtil;

    /**
     * 创建商户实现
     *
     * @param createReqVO 创建信息
     * @return 编号
     */
    @Override
    @GlobalTransactional(rollbackFor = {Exception.class, RuntimeException.class})
    public Long createBusinessInfo(BusinessInfoCreateReqVO createReqVO) {
        //校验是否存在重名商户
        validateBusinessNameExists(createReqVO.getBusinessName());
        // 插入
        BusinessInfoDO businessInfo = BusinessInfoConvert.INSTANCE.convert(createReqVO);
        businessInfo.setBusinessId(CodeEnum.BUSINESS_CODE.getCode());
        businessInfo.setVerifyState((byte) 0);

        businessInfoMapper.insert(businessInfo);

        redisUtil.updateBusinessInfoRedis(businessInfo.getId(), BusinessInfoConvert.INSTANCE.convertToDTO(businessInfo));
        // 返回
        return businessInfo.getId();
    }

    /**
     * 更新商户实现
     *
     * @param updateReqVO 更新信息
     */
    @Override
    public void updateBusinessInfo(BusinessInfoUpdateReqVO updateReqVO) {
        // 校验存在
        validateBusinessInfoExists(updateReqVO.getId());
        // 更新
        BusinessInfoDO updateObj = BusinessInfoConvert.INSTANCE.convert(updateReqVO);
        try {
            businessInfoMapper.updateById(updateObj);
        } catch (Exception e) {
            e.printStackTrace();
            throw exception(BUSINESS_INFO_FAIL_UPDATE);
        }

        redisUtil.updateBusinessInfoRedis(updateObj.getId(), BusinessInfoConvert.INSTANCE.convertToDTO(updateObj));
    }

    /**
     * 删除商户实现
     *
     * @param id 编号
     */
    @Override
    public CommonResult<Boolean> deleteBusinessInfo(Long id) {
        // 校验存在
        validateBusinessInfoExists(id);
        //校验商户是否已认证
        if (businessInfoMapper.selectById(id).getVerifyState() == 1) {
            throw exception(BUSINESS_INFO_FAIL_DEL_IS_VERIFYED);
        }
        // 删除
        try {
            businessInfoMapper.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw exception(BUSINESS_INFO_FAIL_DEL);
        }
        redisUtil.delBusinessInfoRedis(id);
        return success(true);
    }

    /**
     * 根据id,校验商户是否存在
     *
     * @param id 查询条件
     */
    @Override
    public void validateBusinessInfoExists(Long id) {
        if (businessInfoMapper.selectById(id) == null) {
            throw exception(ErrorCodeConstants.BUSINESS_INFO_NOT_EXISTS);
        }
    }

    /**
     * 根据商户编码获取商户信息
     *
     * @param businessId
     * @return
     */
    @Override
    public BusinessInfoDO getBusinessInfoByBusinessId(String businessId) {
        QueryWrapper<BusinessInfoDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("business_id", businessId);
        return businessInfoMapper.selectOne(queryWrapper);
    }

    private void validateBusinessNameExists(String businessName) {
        QueryWrapper<BusinessInfoDO> wrapper = new QueryWrapper<>();
        wrapper.eq("business_name", businessName);
        if (businessInfoMapper.selectOne(wrapper) != null) {
            throw exception(ErrorCodeConstants.BUSINESS_INFO_IS_EXISTS_BUSINESS_NAME);
        }
    }

    /**
     * 获得商户获取
     *
     * @param id 编号
     * @return 商户
     */
    @Override
    public BusinessInfoDO getBusinessInfo(Long id) {
        return businessInfoMapper.selectById(id);
    }

    /**
     * 获得商户分页实现
     *
     * @param pageReqVO 分页查询
     * @return 商户分页
     */
    @Override
    public PageResult<BusinessInfoDO> getBusinessInfoPage(BusinessInfoPageReqVO pageReqVO) {
        return businessInfoMapper.selectPage(pageReqVO);
    }

    /**
     * 获取所有商户昵称实现
     *
     * @return
     */
    @Override
    public List<BusinessInfoDO> getBusinessNameList() {
        return businessInfoMapper.selectBusinessNameList();
    }

}
