package com.xhpc.activity.task;

import com.xhpc.activity.domain.XhpcActivityCashDomain;
import com.xhpc.activity.domain.XhpcActivityRecordDomain;
import com.xhpc.activity.domain.XhpcAppUserDomain;
import com.xhpc.activity.domain.XhpcCommunityPersonnelDomain;
import com.xhpc.activity.domain.XhpcCustomersPersonnelDomain;
import com.xhpc.activity.domain.XhpcUserAccountStatementDomain;
import com.xhpc.activity.enums.ActivityTypeEnum;
import com.xhpc.activity.mapper.XhpcActivityCashMapper;
import com.xhpc.activity.mapper.XhpcActivityDiscountMapper;
import com.xhpc.activity.mapper.XhpcActivityInternetMapper;
import com.xhpc.activity.mapper.XhpcActivityRecordMapper;
import com.xhpc.activity.mapper.XhpcUserMapper;
import com.xhpc.common.core.utils.StringUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


@Component
@Transactional
@RestController
@RequestMapping("/activity/cash/task")
public class AutoActivityTask {

    @Resource
    XhpcActivityCashMapper cashMapper;
    @Resource
    XhpcActivityDiscountMapper discountMapper;
    @Resource
    XhpcActivityInternetMapper internetMapper;

    @Resource
    XhpcUserMapper userMapper;

    @Resource
    XhpcActivityRecordMapper recordMapper;

    @GetMapping("/cashHandle")
    @Scheduled(cron = "0 0 0/6 * * ?")
    private void AutoCashReceive() {
        List<XhpcActivityCashDomain> cashDomainList = cashMapper.selectCheckedPassAndAutoReceive();
        for (XhpcActivityCashDomain cashDomain : cashDomainList) {

            List<XhpcActivityRecordDomain> recordDomainList = new ArrayList<>();
            List<XhpcUserAccountStatementDomain> userAccountStatementDomainList = new ArrayList<>();
            if (cashDomain.getAutoReceive() == 1) {
                // 自动领取
                if (!StringUtils.isEmpty(cashDomain.getAppUserList())) {
                    List<XhpcAppUserDomain> appUserDomainList = new ArrayList<>();
                    if ("-1".equals(cashDomain.getAppUserList())) {
                        appUserDomainList = userMapper.selectAppUsersAll();
                    } else {
                        appUserDomainList = userMapper.selectAppUsersById(cashDomain.getAppUserList());
                    }
                    for (XhpcAppUserDomain userDomain : appUserDomainList) {
                        // 增加领取记录
                        recordDomainList.add(addRecord(cashDomain, 0, userDomain.getAppUserId().toString()));

                        BigDecimal remainingSum = userDomain.getBalance().add(cashDomain.getAmount());
                        // 增加余额
                        userDomain.setBalance(remainingSum);
                        // 增加流水
                        userAccountStatementDomainList.add(
                                addAccountStatement(userDomain.getAppUserId(), cashDomain.getAmount(), remainingSum, 0, userDomain.getTenantId())
                        );
                    }

                    userMapper.updateBatchAppUser(appUserDomainList);
                }

                if (!StringUtils.isEmpty(cashDomain.getComUserList())) {
                    List<XhpcCommunityPersonnelDomain> comUserList = new ArrayList<>();
                    if ("-1".equals(cashDomain.getComUserList())) {
                        comUserList = userMapper.selectComUsersAll();
                    } else {
                        comUserList = userMapper.selectComUsersById(cashDomain.getComUserList());
                    }

                    for (XhpcCommunityPersonnelDomain userDomain : comUserList) {

                        recordDomainList.add(addRecord(cashDomain, 2, userDomain.getCommunityPersonnelId().toString()));

                        BigDecimal redPacket = userDomain.getRedPacket().add(cashDomain.getAmount());
                        BigDecimal surplusMoney = userDomain.getSurplusMoney().add(cashDomain.getAmount());
                        userDomain.setRedPacket(redPacket);
                        userDomain.setSurplusMoney(surplusMoney);

                        userAccountStatementDomainList.add(
                                addAccountStatement(userDomain.getCommunityPersonnelId(), cashDomain.getAmount(), surplusMoney, 2, userDomain.getTenantId())
                        );
                    }

                    userMapper.updateBatchComUser(comUserList);
                }

                if (!StringUtils.isEmpty(cashDomain.getCusUserList())) {

                    List<XhpcCustomersPersonnelDomain> cusUserList = new ArrayList<>();
                    if ("-1".equals(cashDomain.getComUserList())) {
                        cusUserList = userMapper.selectCusUsersAll();
                    } else {
                        cusUserList = userMapper.selectCusUsersById(cashDomain.getCusUserList());
                    }

                    for (XhpcCustomersPersonnelDomain userDomain : cusUserList) {

                        recordDomainList.add(addRecord(cashDomain, 3, userDomain.getCustomersPersonnelId().toString()));

                        BigDecimal redPacket = userDomain.getRedPacket().add(cashDomain.getAmount());
                        BigDecimal surplusMoney = userDomain.getSurplusMoney().add(cashDomain.getAmount());
                        userDomain.setRedPacket(redPacket);
                        userDomain.setSurplusMoney(surplusMoney);

                        userAccountStatementDomainList.add(
                                addAccountStatement(userDomain.getCustomersPersonnelId(), cashDomain.getAmount(), surplusMoney, 3, userDomain.getTenantId())
                        );
                    }

                    userMapper.updateBatchCusUser(cusUserList);
                }

            }

            if (recordDomainList.size() > 0) {
                recordMapper.insertBatch(recordDomainList);
            }
            if (userAccountStatementDomainList.size() > 0) {
                userMapper.insertStatementBatch(userAccountStatementDomainList);
            }

            cashDomain.setReceiveCount(recordDomainList.size());
            cashDomain.setAutoStatus(1);
            cashMapper.updateReceiveCountByActivityId(cashDomain);
        }

    }


    @Scheduled(cron = "0 0 1 * * ?")
    private void updateActivityStatus(){
        cashMapper.updateStatusByExpireNow();
        discountMapper.updateStatusByExpireNow();
        internetMapper.updateStatusByExpireNow();
    }


    private XhpcActivityRecordDomain addRecord(XhpcActivityCashDomain cashDomain, Integer source, String userId) {
        XhpcActivityRecordDomain recordDomain = new XhpcActivityRecordDomain();
        recordDomain.setActivityType(ActivityTypeEnum.CASH.getCode());
        recordDomain.setActivityId(cashDomain.getActivityId());
        recordDomain.setAmount(cashDomain.getAmount());
        recordDomain.setSource(source);
        recordDomain.setUserId(userId);
        return recordDomain;
    }


    private XhpcUserAccountStatementDomain addAccountStatement(Long userId, BigDecimal amount, BigDecimal remainingSum, Integer source, String tenatnId) {
        XhpcUserAccountStatementDomain domain = new XhpcUserAccountStatementDomain();
        domain.setCreateBy("1");
        domain.setUpdateBy("1");
        domain.setType(6);
        domain.setStatus(0);

        domain.setUserId(userId);
        domain.setAmount(amount);
        domain.setRemainingSum(remainingSum);
        domain.setSource(source);
        domain.setTenantId(tenatnId);
        return domain;
    }

}
