package com.hzs.retail.member.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.constant.*;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.exception.ServiceException;
import com.hzs.common.core.service.RedisService;
import com.hzs.common.domain.retail.member.CuMemberRetailRegion;
import com.hzs.common.domain.retail.member.ext.CuMemberRetailRegionExt;
import com.hzs.common.domain.system.base.BdArea;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.retail.member.mapper.CuMemberRetailRegionMapper;
import com.hzs.retail.member.param.RetailMemberRegionParam;
import com.hzs.retail.member.service.ICuMemberRetailRegionService;
import com.hzs.retail.member.vo.UnboundAreaVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 新零售会员区域配置 服务实现类
 */
@Slf4j
@Service
public class CuMemberRetailRegionServiceImpl extends ServiceImpl<CuMemberRetailRegionMapper, CuMemberRetailRegion> implements ICuMemberRetailRegionService {

    @Autowired
    private RedisService redisService;

    @Override
    public CuMemberRetailRegionExt getRegionByMember(Long pkMember, Integer gradeValue, Integer systemType) {
        return baseMapper.getRegionByMember(pkMember, gradeValue, systemType);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String setRegion(RetailMemberRegionParam param) {
        String redisKey = String.format(CacheConstants.RETAIL_REGION, param.getProvince(), param.getCity(), param.getCounty());
        try {
            // 按省、市、区上锁
            if (!redisService.lockKeyMany(redisKey)) {
                return "收益区域绑定失败，请重试";
            }

            if (null != param.getPkId()) {
                // 之前存在数据，将旧数据取消
                this.update(Wrappers.<CuMemberRetailRegion>lambdaUpdate()
                        .eq(CuMemberRetailRegion::getPkMember, param.getPkMember())
                        .eq(CuMemberRetailRegion::getEffective, EYesNo.YES.getIntValue())
                        .eq(CuMemberRetailRegion::getSystemType, param.getSystemType())
                        .set(CuMemberRetailRegion::getEffective, EYesNo.NO.getIntValue())
                        .set(CuMemberRetailRegion::getPkModified, param.getPkMember())
                        .set(CuMemberRetailRegion::getModifiedTime, new Date())
                );
            }
            // 新増数据
            CuMemberRetailRegion retailRegion = CuMemberRetailRegion.builder()
                    .pkMember(param.getPkMember())
                    .province(param.getProvince())
                    .city(param.getCity())
                    .county(param.getCounty())
                    // 数据来源会员端
                    .source(param.getSource())
                    .systemType(param.getSystemType())
                    .build();
            retailRegion.setPkCreator(param.getPkMember());
            retailRegion.setRegionVertexPkId(baseMapper.getRegionVertexPkIdByPkMember(retailRegion.getPkMember()));
            if (baseMapper.insertData(retailRegion) == 0) {
                throw new ServiceException("选择区域失败，请刷新后重试");
            }
        } finally {
            redisService.unlock(redisKey);
        }
        return null;
    }

    @Override
    public List<BdArea> listChooseRegion(Integer systemType) {
        return baseMapper.listChooseRegion(systemType, SecurityUtils.getUserId());
    }

    @Override
    public List<UnboundAreaVO> unboundAreaList(Integer pkCountry, Integer systemType) {
        return baseMapper.unboundAreaList(pkCountry, systemType);
    }

    @Override
    public List<CuMemberRetailRegionExt> memberretailRegList(CuMemberRetailRegionExt mrrExt) {
        return baseMapper.memberretailRegList(mrrExt);
    }

    @Override
    public void cancelRegion(List<Long> pkIdList) {
        this.update(Wrappers.<CuMemberRetailRegion>lambdaUpdate()
                .in(CuMemberRetailRegion::getPkId, pkIdList)
                .eq(CuMemberRetailRegion::getEffective, EYesNo.YES.getIntValue())
                .set(CuMemberRetailRegion::getEffective, EYesNo.NO.getIntValue())
                .set(CuMemberRetailRegion::getPkModified, MagicNumberConstants.PK_ADMIN)
                .set(CuMemberRetailRegion::getModifiedTime, new Date())
        );
    }

    @Override
    public int getOrderCount(Long pkMember, Integer systemType, List<String> waresCodeList) {
        return baseMapper.getOrderCount(pkMember, systemType, waresCodeList);
    }

    @Override
    public int validateBindCount(CuMemberRetailRegionExt mrrExt, Integer yesOrNoValue, Integer systemType, Long pkId) {
        return baseMapper.validateBindCountA(mrrExt, yesOrNoValue, systemType, pkId);
    }

    @Override
    public int validateBindCount(RetailMemberRegionParam mrrExt, Integer yesOrNoValue, Integer systemType, Long pkId) {
        return baseMapper.validateBindCountB(mrrExt, yesOrNoValue, systemType, pkId);
    }

    @Override
    public Long getRegionVertexPkIdByPkMember(Long pkMember) {
        return baseMapper.getRegionVertexPkIdByPkMember(pkMember);
    }

}
