package com.yuchen.saas.park.service.config;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.yuchen.saas.park.api.entity.property.*;
import com.yuchen.saas.park.api.enums.property.EntryStatusEnum;
import com.yuchen.saas.park.api.enums.property.MemberStatusEnum;
import com.yuchen.saas.park.api.enums.property.RegisterStatusEnum;
import com.yuchen.saas.park.api.enums.property.ResidentStatusEnum;
import com.yuchen.saas.park.service.service.property.*;
import lombok.extern.slf4j.Slf4j;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Component
@Slf4j
public class PropertyCustomerManageXxlJobThread {

    @Resource
    private IPropertyHouseResidentService houseResidentService;
    @Resource
    private IPropertyHouseEnterpriseService houseEnterpriseService;
    @Resource
    private IPropertyEnterpriseAddressService enterpriseAddressService;
    @Resource
    private IPropertyEnterpriseMemberService enterpriseMemberService;
    @Resource
    private IPropertyUserHouseService userHouseService;

    public final static String UPDATE_EXPIRED_STATUS_JOB_HANDLER = "updateExpiredStatusJobHandler";

    @XxlJob(UPDATE_EXPIRED_STATUS_JOB_HANDLER)
    public void updateExpiredStatusJobHandler() {
        log.info("PropertyCustomerManageXxlJobThread execution succeeded");
        List<PropertyHouseResident> updateRegisterStatusResidentList = houseResidentService.lambdaQuery().eq(PropertyHouseResident::getRegisterStatus, RegisterStatusEnum.REGISTERED.getValue())
                .le(PropertyHouseResident::getEndDate, new Date()).list();
        //登记状态更新
        if (Func.isNotEmpty(updateRegisterStatusResidentList)) {
            LambdaUpdateWrapper<PropertyHouseResident> residentUpdateWrapper = Wrappers.lambdaUpdate();
            residentUpdateWrapper.eq(PropertyHouseResident::getRegisterStatus, RegisterStatusEnum.REGISTERED.getValue());
            residentUpdateWrapper.le(PropertyHouseResident::getEndDate, new Date());
            residentUpdateWrapper.set(PropertyHouseResident::getRegisterStatus, RegisterStatusEnum.EXPIRED.getValue());
            houseResidentService.update(residentUpdateWrapper);
            //更新小程序用户/企业端状态
            List<Long> idList = updateRegisterStatusResidentList.stream().map(PropertyHouseResident::getId).collect(Collectors.toList());
            LambdaUpdateWrapper<PropertyUserHouse> userHouseUpdateWrapper = Wrappers.lambdaUpdate();
            userHouseUpdateWrapper.in(PropertyUserHouse::getLinkId, idList);
            userHouseUpdateWrapper.set(PropertyUserHouse::getIsInvalid, 1);
            userHouseService.update(userHouseUpdateWrapper);
        }
        //住户状态更新
        List<PropertyHouseResident> updateEntryStatusResidentList = houseResidentService.lambdaQuery().eq(PropertyHouseResident::getResidentStatus, ResidentStatusEnum.NORMAL.getValue())
                .le(PropertyHouseResident::getEndDate, new Date()).list();
        if (Func.isNotEmpty(updateEntryStatusResidentList)) {
            LambdaUpdateWrapper<PropertyHouseResident> residentUpdateWrapper = Wrappers.lambdaUpdate();
            residentUpdateWrapper.eq(PropertyHouseResident::getResidentStatus, ResidentStatusEnum.NORMAL.getValue());
            residentUpdateWrapper.le(PropertyHouseResident::getEndDate, new Date());
            residentUpdateWrapper.set(PropertyHouseResident::getResidentStatus, ResidentStatusEnum.INVALID.getValue());
            houseResidentService.update(residentUpdateWrapper);
        }
        //登记状态更新
        List<PropertyHouseEnterprise> updateRegisterStatusEnterpriseList = houseEnterpriseService.lambdaQuery().eq(PropertyHouseEnterprise::getRegisterStatus, RegisterStatusEnum.REGISTERED.getValue())
                .le(PropertyHouseEnterprise::getEndDate, new Date()).list();
        if (Func.isNotEmpty(updateRegisterStatusEnterpriseList)) {
            LambdaUpdateWrapper<PropertyHouseEnterprise> enterpriseUpdateWrapper = Wrappers.lambdaUpdate();
            enterpriseUpdateWrapper.eq(PropertyHouseEnterprise::getRegisterStatus, RegisterStatusEnum.REGISTERED.getValue());
            enterpriseUpdateWrapper.le(PropertyHouseEnterprise::getEndDate, new Date());
            enterpriseUpdateWrapper.set(PropertyHouseEnterprise::getRegisterStatus, RegisterStatusEnum.EXPIRED.getValue());
            houseEnterpriseService.update(enterpriseUpdateWrapper);
            //更新小程序企业状态
            updateRegisterStatusEnterpriseList.forEach(houseEnterprise -> {
                LambdaUpdateWrapper<PropertyEnterpriseAddress> enterpriseAddrUpdateWrapper = Wrappers.lambdaUpdate();
                enterpriseAddrUpdateWrapper.eq(PropertyEnterpriseAddress::getLinkId, houseEnterprise.getId());
                enterpriseAddrUpdateWrapper.set(PropertyEnterpriseAddress::getIsValid, 1);
                enterpriseAddressService.update(enterpriseAddrUpdateWrapper);
            });
            //更新企业成员状态
            List<Long> enterIdList = updateRegisterStatusEnterpriseList.stream().map(PropertyHouseEnterprise::getId).collect(Collectors.toList());
            LambdaUpdateWrapper<PropertyEnterpriseMember> memberUpdateWrapper = Wrappers.lambdaUpdate();
            memberUpdateWrapper.in(PropertyEnterpriseMember::getEnterpriseId, enterIdList);
            memberUpdateWrapper.set(PropertyEnterpriseMember::getRegisterStatus, RegisterStatusEnum.EXPIRED.getValue());
            memberUpdateWrapper.set(PropertyEnterpriseMember::getMemberStatus, MemberStatusEnum.INVALID.getValue());
            enterpriseMemberService.update(memberUpdateWrapper);
            //todo 更新小程序企业成员状态
        }
        //入驻状态更新
        List<PropertyHouseEnterprise> updateEntryStatusEnterpriseList = houseEnterpriseService.lambdaQuery().eq(PropertyHouseEnterprise::getEntryStatus, EntryStatusEnum.ENTRY.getValue())
                .le(PropertyHouseEnterprise::getEndDate, new Date()).list();
        if (Func.isNotEmpty(updateEntryStatusEnterpriseList)) {
            LambdaUpdateWrapper<PropertyHouseEnterprise> enterpriseUpdateWrapper = Wrappers.lambdaUpdate();
            enterpriseUpdateWrapper.eq(PropertyHouseEnterprise::getEntryStatus, EntryStatusEnum.ENTRY.getValue());
            enterpriseUpdateWrapper.le(PropertyHouseEnterprise::getEndDate, new Date());
            enterpriseUpdateWrapper.set(PropertyHouseEnterprise::getEntryStatus, EntryStatusEnum.EXPIRED.getValue());
            houseEnterpriseService.update(enterpriseUpdateWrapper);
            //更新企业成员状态
            List<Long> enterIdList = updateRegisterStatusEnterpriseList.stream().map(PropertyHouseEnterprise::getId).collect(Collectors.toList());
            LambdaUpdateWrapper<PropertyEnterpriseMember> memberUpdateWrapper = Wrappers.lambdaUpdate();
            memberUpdateWrapper.in(PropertyEnterpriseMember::getEnterpriseId, enterIdList);
            memberUpdateWrapper.set(PropertyEnterpriseMember::getRegisterStatus, RegisterStatusEnum.EXPIRED.getValue());
            memberUpdateWrapper.set(PropertyEnterpriseMember::getMemberStatus, MemberStatusEnum.INVALID.getValue());
            enterpriseMemberService.update(memberUpdateWrapper);
        }
    }


}