package com.yuchen.saas.park.service.listener.property;

import com.yuchen.saas.manage.user.api.entity.User;
import com.yuchen.saas.manage.user.api.feign.IUserClient;
import com.yuchen.saas.park.api.entity.park.PmgBatParkInfo;
import com.yuchen.saas.park.api.entity.property.PropertyFeeItem;
import com.yuchen.saas.park.api.entity.property.PropertyFeeStandard;
import com.yuchen.saas.park.api.entity.property.PropertyHouses;
import com.yuchen.saas.park.api.enums.property.CustomerTypeEnum;
import com.yuchen.saas.park.api.vo.property.PropertyCustomerArchivesVo;
import com.yuchen.saas.park.api.vo.property.PropertyHouseParentInfoVo;
import com.yuchen.saas.park.service.event.property.*;
import com.yuchen.saas.park.service.service.PmgBatParkInfoService;
import com.yuchen.saas.park.service.service.property.IPropertyCustomerArchivesService;
import com.yuchen.saas.park.service.service.property.IPropertyFeeItemService;
import com.yuchen.saas.park.service.service.property.PropertyFeeStandardService;
import com.yuchen.saas.park.service.service.property.PropertyHousesService;
import org.nest.springwrap.core.tool.utils.ClassUtil;
import org.nest.springwrap.core.tool.utils.Func;
import org.nest.springwrap.core.tool.utils.ObjectUtil;
import org.nest.springwrap.core.tool.utils.ReflectUtil;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;

/**
 * 列表信息设置
 */
@Service
public class SetPropertyDataInfoEventListener {
    @Resource
    private PropertyHousesService propertyHousesService;
    @Resource
    private PropertyFeeStandardService propertyFeeStandardService;
    @Resource
    private IPropertyFeeItemService propertyFeeItemService;
    @Resource
    private PmgBatParkInfoService pmgBatParkInfoService;
    @Resource
    private IPropertyCustomerArchivesService propertyCustomerArchivesService;
    @Resource
    private IUserClient userClient;

    /**
     * 监听设置房源名称，资源代码信息
     *
     * @param event
     * @throws Exception
     */
    @EventListener
    public void setHouseNameResourceCode(SetHouseNameResourceCodeEvent event) throws Exception {
        List<Long> houseIdList = event.getHouseIdList();
        List<?> dataList = event.getDataList();
        if (Func.isEmpty(houseIdList) || Func.isEmpty(dataList)) {
            return;
        }
        List<PropertyHouses> houseList = propertyHousesService.listByIds(houseIdList);
        if (Func.isEmpty(houseList)) {
            return;
        }

        try {
            Object tmpData = dataList.get(0);
            Field houseId = ReflectUtil.getField(tmpData.getClass(), "houseId");
            if (ObjectUtil.isEmpty(houseId)) {
                return;
            }
            for (Object data : dataList) {
                Method getHouseId = ClassUtil.getMethod(data.getClass(), "getHouseId", new Class[0]);
                String dataHouseId = String.valueOf(getHouseId.invoke(data));
                for (PropertyHouses house : houseList) {
                    if (dataHouseId.equals(String.valueOf(house.getId()))) {
                        Field resourceCode = ReflectUtil.getField(data.getClass(), "resourceCode");
                        if (ObjectUtil.isNotEmpty(resourceCode)) {
                            Method setResourceCode = ClassUtil.getMethod(data.getClass(), "setResourceCode", new Class[]{String.class});
                            setResourceCode.invoke(data, house.getResourceCode());
                        }
                        Field houseName = ReflectUtil.getField(data.getClass(), "houseName");
                        if (ObjectUtil.isNotEmpty(houseName)) {
                            Method setHouseName = ClassUtil.getMethod(data.getClass(), "setHouseName", new Class[]{String.class});
                            setHouseName.invoke(data, house.getHouseName());
                        }
                    }
                }
            }
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     * 根据房源id 设置 房源名称，房源代码，楼栋名称，园区名称
     */
    @EventListener
    public void setHouseParentInfo(SetHouseParentInfoEvent event) throws Exception {
        List<Long> houseIdList = event.getHouseIdList();
        List<?> dataList = event.getDataList();
        if (Func.isEmpty(houseIdList) || Func.isEmpty(dataList)) {
            return;
        }
        List<PropertyHouseParentInfoVo> houseList = propertyHousesService.listHouseParentInfo(houseIdList);
        if (Func.isEmpty(houseList)) {
            return;
        }

        try {
            Object tmpData = dataList.get(0);
            Field houseId = ReflectUtil.getField(tmpData.getClass(), "houseId");
            if (ObjectUtil.isEmpty(houseId)) {
                return;
            }
            for (Object data : dataList) {
                Method getHouseId = ClassUtil.getMethod(data.getClass(), "getHouseId", new Class[0]);
                String dataHouseId = String.valueOf(getHouseId.invoke(data));
                for (PropertyHouseParentInfoVo house : houseList) {
                    if (dataHouseId.equals(String.valueOf(house.getHouseId()))) {
                        Field resourceCode = ReflectUtil.getField(data.getClass(), "resourceCode");
                        if (ObjectUtil.isNotEmpty(resourceCode)) {
                            Method setResourceCode = ClassUtil.getMethod(data.getClass(), "setResourceCode", new Class[]{String.class});
                            setResourceCode.invoke(data, house.getResourceCode());
                        }
                        Field houseName = ReflectUtil.getField(data.getClass(), "houseName");
                        if (ObjectUtil.isNotEmpty(houseName)) {
                            Method setHouseName = ClassUtil.getMethod(data.getClass(), "setHouseName", new Class[]{String.class});
                            setHouseName.invoke(data, house.getHouseName());
                        }
                        Field buildingName = ReflectUtil.getField(data.getClass(), "buildingName");
                        if (ObjectUtil.isNotEmpty(buildingName)) {
                            Method setBuildingName = ClassUtil.getMethod(data.getClass(), "setBuildingName", new Class[]{String.class});
                            setBuildingName.invoke(data, house.getBuildingName());
                        }
                        Field parkName = ReflectUtil.getField(data.getClass(), "parkName");
                        if (ObjectUtil.isNotEmpty(parkName)) {
                            Method setParkName = ClassUtil.getMethod(data.getClass(), "setParkName", new Class[]{String.class});
                            setParkName.invoke(data, house.getParkName());
                        }
                    }
                }
            }
        } catch (Exception e) {
            throw e;
        }
    }


    /**
     * 监听设置收费标准名称
     *
     * @param event
     * @throws Exception
     */
    @EventListener
    public void setFeeName(SetFeeNameEvent event) throws Exception {
        List<Long> feeIdList = event.getFeeIdList();
        List<?> dataList = event.getDataList();
        if (Func.isEmpty(feeIdList) || Func.isEmpty(dataList)) {
            return;
        }
        List<PropertyFeeStandard> feeList = propertyFeeStandardService.listByIds(feeIdList);
        if (Func.isEmpty(feeList)) {
            return;
        }

        try {
            Object tmpData = dataList.get(0);
            Field feeId = ReflectUtil.getField(tmpData.getClass(), "feeId");
            if (ObjectUtil.isEmpty(feeId)) {
                return;
            }
            for (Object data : dataList) {
                Method getFeeId = ClassUtil.getMethod(data.getClass(), "getFeeId", new Class[0]);
                String dataFeeId = String.valueOf(getFeeId.invoke(data));
                for (PropertyFeeStandard fee : feeList) {
                    if (dataFeeId.equals(String.valueOf(fee.getId()))) {
                        Field feeName = ReflectUtil.getField(data.getClass(), "feeName");
                        if (ObjectUtil.isNotEmpty(feeName)) {
                            Method setFeeName = ClassUtil.getMethod(data.getClass(), "setFeeName", new Class[]{String.class});
                            setFeeName.invoke(data, fee.getFeeName());
                        }
                    }
                }
            }
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     * 设置费用项目名称
     * @param event
     * @throws Exception
     */
    @EventListener
    public void SetFeeItemName(SetFeeItemNameEvent event) throws Exception {
        List<Long> feeItemIdList = event.getFeeItemIdList();
        List<?> dataList = event.getDataList();
        if (Func.isEmpty(feeItemIdList) || Func.isEmpty(dataList)) {
            return;
        }
        List<PropertyFeeItem> feeItemList = propertyFeeItemService.listByIds(feeItemIdList);
        if (Func.isEmpty(feeItemList)) {
            return;
        }

        try {
            Object tmpData = dataList.get(0);
            Field feeItemId = ReflectUtil.getField(tmpData.getClass(), "feeItemId");
            if (ObjectUtil.isEmpty(feeItemId)) {
                return;
            }
            for (Object data : dataList) {
                Method getFeeItemId = ClassUtil.getMethod(data.getClass(), "getFeeItemId", new Class[0]);
                String dataFeeId = String.valueOf(getFeeItemId.invoke(data));
                for (PropertyFeeItem feeItem : feeItemList) {
                    if (dataFeeId.equals(String.valueOf(feeItem.getId()))) {
                        Field feeItemName = ReflectUtil.getField(data.getClass(), "feeItemName");
                        if (ObjectUtil.isNotEmpty(feeItemName)) {
                            Method setFeeItemName = ClassUtil.getMethod(data.getClass(), "setFeeItemName", new Class[]{String.class});
                            setFeeItemName.invoke(data, feeItem.getFeeItemName());
                        }
                    }
                }
            }
        } catch (Exception e) {
            throw e;
        }
    }


    /**
     * 设置园区名称
     *
     * @param event
     * @throws Exception
     */
    @EventListener
    public void setParkName(SetParkNameEvent event) throws Exception {
        List<Long> parkIdList = event.getParkIdList();
        List<?> dataList = event.getDataList();
        if (Func.isEmpty(parkIdList) || Func.isEmpty(dataList)) {
            return;
        }
        List<PmgBatParkInfo> parkList = pmgBatParkInfoService.listByIds(parkIdList);
        if (Func.isEmpty(parkList)) {
            return;
        }

        try {
            Object tmpData = dataList.get(0);
            Field parkId = ReflectUtil.getField(tmpData.getClass(), "parkId");
            if (ObjectUtil.isEmpty(parkId)) {
                return;
            }
            for (Object data : dataList) {
                Method getParkId = ClassUtil.getMethod(data.getClass(), "getParkId", new Class[0]);
                String dataParkId = String.valueOf(getParkId.invoke(data));
                for (PmgBatParkInfo park : parkList) {
                    if (dataParkId.equals(String.valueOf(park.getId()))) {
                        Field parkName = ReflectUtil.getField(data.getClass(), "parkName");
                        if (ObjectUtil.isNotEmpty(parkName)) {
                            Method setParkName = ClassUtil.getMethod(data.getClass(), "setParkName", new Class[]{String.class});
                            setParkName.invoke(data, park.getParkName());
                        }
                    }
                }
            }
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     * 设置客户信息
     * @param event
     * @throws Exception
     */
    @EventListener
    public void setCustomerInfo(SetCustomerInfoEvent event) throws Exception {
        List<Long> customerIdList = event.getCustomerIdList();
        List<?> dataList = event.getDataList();
        if (Func.isEmpty(customerIdList) || Func.isEmpty(dataList)) {
            return;
        }
        List<PropertyCustomerArchivesVo> caList = propertyCustomerArchivesService.getCustomerArchivesByIdList(customerIdList);
        if (Func.isEmpty(caList)) {
            return;
        }

        try {
            Object tmpData = dataList.get(0);
            Field customerId = ReflectUtil.getField(tmpData.getClass(), "customerId");
            if (ObjectUtil.isEmpty(customerId)) {
                return;
            }
            for (Object data : dataList) {
                Method getCustomerId = ClassUtil.getMethod(data.getClass(), "getCustomerId", new Class[0]);
                String dataId = String.valueOf(getCustomerId.invoke(data));
                for (PropertyCustomerArchivesVo archivesVo : caList) {
                    if (dataId.equals(String.valueOf(archivesVo.getId()))) {
                        Field customerName = ReflectUtil.getField(data.getClass(), "customerName");
                        if (ObjectUtil.isNotEmpty(customerName)) {
                            Method setCustomerName = ClassUtil.getMethod(data.getClass(), "setCustomerName", new Class[]{String.class});
                            setCustomerName.invoke(data, archivesVo.getCustomerName());
                        }
                        Field customerType = ReflectUtil.getField(data.getClass(), "customerType");
                        if (ObjectUtil.isNotEmpty(customerType)) {
                            Method setCustomerType = ClassUtil.getMethod(data.getClass(), "setCustomerType", new Class[]{Integer.class});
                            setCustomerType.invoke(data, archivesVo.getCustomerType());
                        }
                        Field customerTypeName = ReflectUtil.getField(data.getClass(), "customerTypeName");
                        if (ObjectUtil.isNotEmpty(customerTypeName)) {
                            Method setCustomerTypeName = ClassUtil.getMethod(data.getClass(), "setCustomerTypeName", new Class[]{String.class});
                            setCustomerTypeName.invoke(data, CustomerTypeEnum.getDescByCode(archivesVo.getCustomerType()));
                        }
                    }
                }
            }
        } catch (Exception e) {
            throw e;
        }
    }


    @EventListener
    public void setCreateUserName(SetCreateUserNameEvent event) throws Exception {
        List<Long> userIdList = event.getUserIdList();
        List<?> dataList = event.getDataList();
        if (Func.isEmpty(userIdList) || Func.isEmpty(dataList)) {
            return;
        }
        List<User> userList = userClient.findUserListByUserIds(userIdList).getData();
        if (Func.isEmpty(userList)) {
            return;
        }

        try {
            Object tmpData = dataList.get(0);
            Field createUser = ReflectUtil.getField(tmpData.getClass(), "createUser");
            if (ObjectUtil.isEmpty(createUser)) {
                return;
            }
            for (Object data : dataList) {
                Method getCreateUser = ClassUtil.getMethod(data.getClass(), "getCreateUser", new Class[0]);
                String dataId = String.valueOf(getCreateUser.invoke(data));
                for (User vo : userList) {
                    if (dataId.equals(String.valueOf(vo.getId()))) {
                        Field createUserName = ReflectUtil.getField(data.getClass(), "createUserName");
                        if (ObjectUtil.isNotEmpty(createUserName)) {
                            Method setCreateUserName = ClassUtil.getMethod(data.getClass(), "setCreateUserName", new Class[]{String.class});
                            setCreateUserName.invoke(data, vo.getName());
                        }
                    }
                }
            }
        } catch (Exception e) {
            throw e;
        }
    }

}
