package cn.ysk.cashier.mybatis.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.ysk.cashier.exception.BadRequestException;
import cn.ysk.cashier.mybatis.mapper.MpUserInfoMapper;
import cn.ysk.cashier.mybatis.mapper.ShopUserMapper;
import cn.ysk.cashier.mybatis.service.MemberService;
import cn.ysk.cashier.pojo.TbUserInfo;
import cn.ysk.cashier.pojo.shop.TbShopUser;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

/**
 * 会员管理
 *
 * @author tankaikai
 * @since 2024-10-24 16:16
 */
@Service
public class MemberServiceImpl implements MemberService {

    @Resource
    private ShopUserMapper shopUserMapper;
    @Resource
    private MpUserInfoMapper mpUserInfoMapper;

    @Override
    public void createMember(Map<String, Object> map) {
        if (ObjectUtil.isEmpty(map) || !map.containsKey("shopId") || ObjectUtil.isEmpty(map.get("shopId"))
                || !map.containsKey("phone") || ObjectUtil.isEmpty(map.get("phone"))
                || !map.containsKey("nickName") || ObjectUtil.isEmpty(map.get("nickName"))
                || !map.containsKey("sex") || ObjectUtil.isEmpty(map.get("sex"))
                || !map.containsKey("level") || ObjectUtil.isEmpty(map.get("level")) ||
                !map.containsKey("birthDay") || ObjectUtil.isEmpty(map.get("birthDay"))
        ) {
            throw new BadRequestException("请求参数不允许为空");
        }
        String phone = String.valueOf(map.get("phone"));

        String shopId = String.valueOf(map.get("shopId"));

        List<TbShopUser> tbShopUsers = shopUserMapper.selectList(Wrappers.<TbShopUser>lambdaQuery().eq(TbShopUser::getShopId, shopId).eq(TbShopUser::getTelephone, phone));

        if (ObjectUtil.isNotEmpty(tbShopUsers) && tbShopUsers.stream().filter(it -> "1".equals(it.getIsVip().toString())).count() > 0) {
            throw new BadRequestException("会员已存在");
        }

        Long count = mpUserInfoMapper.selectCount(Wrappers.<TbUserInfo>lambdaQuery().eq(TbUserInfo::getTelephone, phone));
        if (count > 1) {
            throw new BadRequestException("相同的手机号已存在多个记录");
        }

        if (ObjectUtil.isNotNull(tbShopUsers) && ObjectUtil.isNotEmpty(tbShopUsers)) {
            TbShopUser tbShopUser = tbShopUsers.get(0);
            tbShopUser.setTelephone(phone);
            tbShopUser.setBirthDay(String.valueOf(map.get("birthDay")));
            tbShopUser.setName(String.valueOf(map.get("nickName")));
            tbShopUser.setSex(Convert.toInt(map.get("sex")));
            tbShopUser.setLevel(Convert.toInt(map.get("level")));
            tbShopUser.setIsVip(1);
            tbShopUser.setUpdatedAt(System.currentTimeMillis());
            tbShopUser.setJoinTime(new Timestamp(System.currentTimeMillis()));
            shopUserMapper.updateById(tbShopUser);
            return;
        }

        TbShopUser tbShopUser = new TbShopUser();
        TbUserInfo tbUserInfo = mpUserInfoMapper.selectOne(Wrappers.<TbUserInfo>lambdaQuery().eq(TbUserInfo::getTelephone, phone));
        if (tbUserInfo != null) {
            tbShopUser.setUserId(tbUserInfo.getId().toString());
        }
        tbShopUser.setAmount(BigDecimal.ZERO);
        tbShopUser.setCreditAmount(BigDecimal.ZERO);
        tbShopUser.setConsumeAmount(BigDecimal.ZERO);
        tbShopUser.setConsumeNumber(0);
        tbShopUser.setLevelConsume(BigDecimal.ZERO);
        tbShopUser.setStatus(1);
        tbShopUser.setShopId(shopId);
        tbShopUser.setTelephone(phone);
        tbShopUser.setBirthDay(String.valueOf(map.get("birthDay")));
        tbShopUser.setName(String.valueOf(map.get("nickName")));
        tbShopUser.setSex(Convert.toInt((map.get("sex"))));
        tbShopUser.setLevel(Convert.toInt(map.get("level")));
        String code = RandomUtil.randomNumbers(8);
        tbShopUser.setCode(code);
        tbShopUser.setIsVip(1);
        tbShopUser.setCreatedAt(System.currentTimeMillis());
        tbShopUser.setJoinTime(new Timestamp(System.currentTimeMillis()));
        shopUserMapper.insert(tbShopUser);
    }
}
