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

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.yuchen.saas.park.api.dto.property.CreateBillDto;
import com.yuchen.saas.park.api.dto.property.PropertyCycleCreateBillDto;
import com.yuchen.saas.park.api.entity.property.PropertyHouseBindFee;
import com.yuchen.saas.park.api.enums.property.CreateBillTypeEnum;
import com.yuchen.saas.park.service.service.property.IPropertyHouseBindFeeService;
import com.yuchen.saas.park.service.service.property.bill.PropertyBillFactoryService;
import lombok.extern.slf4j.Slf4j;
import org.nest.springwrap.core.mp.support.Query;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * 周期性账单
 */
@Slf4j
@Component
public class PropertyCycleBillJob extends BaseXxlJob {
    @Resource
    private IPropertyHouseBindFeeService propertyHouseBindFeeService;
    @Resource
    private PropertyBillFactoryService propertyBillFactoryService;


    @XxlJob("propertyCycleBillJob")
    public void handle() {
        XxlJobHelper.log(">>>>>>>>>PropertyCycleBillJobStart date = " + DateUtil.formatDateTime(new Date()));
        Map<String,String> paramMap = super.getJobParams();
        XxlJobHelper.log(">>>>>>>>>paramMap testData = " + paramMap.get("testDate"));
        Query query = new Query();
        query.setCurrent(-1);
        query.setSize(-1);
        // TODO tucq 优化分页查询
        Page<PropertyHouseBindFee> page = propertyHouseBindFeeService.searchHouseBindCycleFeePage(query);
        XxlJobHelper.log(">>>>>>>>>PropertyCycleBillJobSize = " + page.getRecords().size());
        int i = 0;
        for (PropertyHouseBindFee item : page.getRecords()) { // TODO tucq 优化 MQ 发送
            PropertyCycleCreateBillDto cycleCreateBillDto = new PropertyCycleCreateBillDto();
            cycleCreateBillDto.setHouseId(item.getHouseId());
            cycleCreateBillDto.setFeeId(item.getFeeStandardId());
            if (Func.isNotEmpty(paramMap.get("testDate"))) {
                cycleCreateBillDto.setTestDate(paramMap.get("testDate"));
            }
            CreateBillDto createBillDto = new CreateBillDto();
            createBillDto.setCreateBillTypeEnum(CreateBillTypeEnum.CYCLE);
            createBillDto.setCycleCreateBillDto(cycleCreateBillDto);
            try {
                propertyBillFactoryService.createBill(createBillDto);
                XxlJobHelper.log(">>>>>>>>> Success houseId = " + item.getHouseId());
                i ++;
            } catch (Exception e) {
                XxlJobHelper.log("===>> ERROR PropertyCycleBillJobHouseId="+item.getHouseId());
                XxlJobHelper.log(e);
            }
        }
        XxlJobHelper.log("===========周期性账单共成功生成 " + i + "条===========");
        XxlJobHelper.log(">>>>>>>>>PropertyCycleBillJobEnd date = " + DateUtil.formatDateTime(new Date()));

    }


}
