package com.cyksj.service.groups.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyksj.common.constant.Constant;
import com.cyksj.mapper.AccountMapper;
import com.cyksj.mapper.GroupsMapper;
import com.cyksj.mapper.GroupsRelationMapper;
import com.cyksj.model.entity.Account;
import com.cyksj.model.entity.GoodsDonSku;
import com.cyksj.model.entity.GroupsRelation;
import com.cyksj.model.entity.GroupsTrips;
import com.cyksj.model.manage.views.AccountView;
import com.cyksj.service.groups.GroupsFuncService;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.util.MapUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

/*
 *项目名: netflix
 *文件名: GroupFunServiceImpl
 *创建者: JavaZou
 *创建时间:2023/6/8 16:34
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class GroupFunServiceImpl implements GroupsFuncService {

	private final BeanSearcher beanSearcher;

	private final GroupsMapper groupsMapper;

	private final GroupsRelationMapper groupsRelationMapper;

	private final AccountMapper accountMapper;

	@Override
	public GroupsRelation createNewGroupsTrips(GoodsDonSku sku) {
		//新增车次
		GroupsTrips groups = new GroupsTrips();
		groups.setSkuId(sku.getId());
		groups.setNum(sku.getNum());
		groups.setAvailableNum(sku.getNum());
		if (sku.getGoodsId() == 23) {
			AccountView accountView = beanSearcher.searchFirst(AccountView.class, MapUtils.builder().field(AccountView::getGoodsId, 23).onlySelect(AccountView::getId).build());
			if (accountView != null) {
				groups.setAccountId(accountView.getId());
				groups.setStatus(GroupsTrips.Status.validity);
				groups.setRemark("已设置唯一账号");
			} else {
				groups.setStatus(GroupsTrips.Status.waiting);
				groups.setRemark("等待设置账号");
			}
		} else {
			groups.setStatus(GroupsTrips.Status.waiting);
			groups.setRemark("等待设置账号");
		}
		//限制每天新车队前3天 停车数量
		//10人镜像月付GPT
		Integer availableParkingNum = 0;
		if (sku.getId() == 161l || sku.getId() == 422l) {
			availableParkingNum = 20;
		}
		//4人镜像月付GPT
		if (sku.getId() == 178l || sku.getId() == 423l) {
			availableParkingNum = 6;
		}
		//如果是镜像车队
		setMirrorUnifyAccount(sku, groups);
		groups.setAvailableParkingNum(availableParkingNum);

		groupsMapper.insert(groups);
		GroupsRelation relation = new GroupsRelation();
		relation.setGroupsId(groups.getId());
		relation.setNum(1);
		relation.setStatus(GroupsRelation.Status.none);
		relation.setGroupsId(groups.getId());
		groupsRelationMapper.insert(relation);

		for (int i = 2; i <= groups.getNum(); i++) {
			GroupsRelation groupsRelation = new GroupsRelation();
			groupsRelation.setGroupsId(groups.getId());
			groupsRelation.setNum(i);
			groupsRelation.setStatus(GroupsRelation.Status.none);
			groupsRelationMapper.insert(groupsRelation);
		}
		return relation;
	}

	/**
	 * 设置镜像统一账号
	 */
	public void setMirrorUnifyAccount(GoodsDonSku sku, GroupsTrips groups) {
		//如果是GPT、MJ镜像车队
		//设置唯一账号
		Long goodsId = sku.getGoodsId();
		Boolean isMirror = sku.getIsMirror();
		Boolean isCar = sku.getIsCar();
		String account = null;
		if ((goodsId == Constant.CODEX_GOODS_ID) || (goodsId == Constant.NANO_GOODS_ID) || (goodsId == Constant.CLAUDE_CODE_GOODS_ID) || (goodsId == 88 && isMirror) || (goodsId == 87 && isMirror) || (goodsId == 18 && isCar) || (goodsId == 26 && isMirror) || (goodsId == 75 && isCar)) {
			if (goodsId == 18) {
				account = Constant.GPT_CAR_ONLY_ACCOUNT;
			}
			if (goodsId == 26) {
				account = Constant.MJ_MIRROR_ONLY_ACCOUNT;
			}
			if (goodsId == 75) {
				account = Constant.CLAUDE_MIRROR_ONLY_ACCOUNT;
			}
			if (goodsId == 87) {
				account = Constant.LUMA_MIRROR_ONLY_ACCOUNT;
			}
			if (goodsId == 88) {
				account = Constant.GROK_MIRROR_ONLY_ACCOUNT;
			}
			//claude code
			if (goodsId == Constant.CLAUDE_CODE_GOODS_ID) {
				account = Constant.CLAUDE_CODE_ONLY_ACCOUNT;
			}
			//nano code
			if (goodsId == Constant.NANO_GOODS_ID) {
				account = Constant.NANO_ONLY_ACCOUNT;
			}
			//codex
			if (goodsId == Constant.CODEX_GOODS_ID) {
				account = Constant.CODEX_ONLY_ACCOUNT;
			}
			AccountView accountView = beanSearcher.searchFirst(AccountView.class, MapUtils.builder()
					.field(AccountView::getGoodsId, goodsId)
					.field(AccountView::getAccount, account).
					onlySelect(AccountView::getId).build());
			Long accountId;
			if (accountView == null) {
				try {
					Account insert = new Account();
					insert.setAccount(account);
					insert.setPassword(account);
					insert.setGoodsId(goodsId);
					accountMapper.insert(insert);
					accountId = insert.getId();
				} catch (DuplicateKeyException e) {
					accountId = accountMapper.selectOne(Wrappers.lambdaQuery(Account.class)
							.eq(Account::getGoodsId, goodsId)
							.eq(Account::getAccount, account)
							.last("limit 1")).getId();
				}
			} else {
				accountId = accountView.getId();
			}
			groups.setAccountId(accountId);
			groups.setStatus(GroupsTrips.Status.validity);
			groups.setRemark("已设置镜像车队唯一账号");
		}
	}
}
