package com.willalp.app.domain.service;

import cn.hutool.core.util.ObjectUtil;
import com.willalp.app.domain.HsAppHomeData;
import com.willalp.app.domain.vo.AppConfigVo;
import com.willalp.app.service.IHsAppHomeDataService;
import com.willalp.common.exception.base.BaseException;
import com.willalp.common.utils.DateUtils;

import java.util.List;
import java.util.function.Predicate;

/**
 * @author hs
 */
public class AppConfigSaveThreadTools implements Runnable {

    /**
     * 本次操作的子项
     */
    private final List<HsAppHomeData> list;

    /**
     * 本次操作需要比对删除的子项
     */
    private final List<HsAppHomeData> removeList;

    /**
     * 父级ID（首页方案ID）
     */
    private final String homeParentId;

    private final IHsAppHomeDataService appHomeDataService;

    public AppConfigSaveThreadTools(AppConfigVo appConfig, Integer homeGroupType, List<HsAppHomeData> removeList,
                                    IHsAppHomeDataService dataService) throws BaseException {
        //注入Service
        this.appHomeDataService = dataService;
        //获取本次方案的ID作为子项的父ID
        this.homeParentId = String.valueOf(appConfig.getHomeContentId());
        //根据类型判断
        switch (homeGroupType) {
            case 1:
                this.list = appConfig.getTopImg();
                this.removeList = removeList;
                break;
            case 2:
                this.list = appConfig.getMiddleIcon();
                this.removeList = removeList;
                break;
            case 3:
                this.list = appConfig.getBottomList();
                this.removeList = removeList;
                break;
            default:
                throw new BaseException("app.home.config.type.null", new Object[]{homeGroupType});
        }
    }

    @Override
    public void run() {
        try {
            saveList();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void saveList() {
        boolean result = true;
        for (HsAppHomeData appHomeData : list) {
            if (ObjectUtil.isNotNull(appHomeData)) {
                Predicate<HsAppHomeData> homeDataPredicate =
                        (item) -> item.getHomeContentId().equals(appHomeData.getHomeContentId());
                this.removeList.removeIf(homeDataPredicate);
            }
//            HsAppHomeData appHomeData = new HsAppHomeData();
//            BeanUtil.copyProperties(appHomeData, appHomeData);
            try {
                appHomeData.setUpdateTime(DateUtils.getNowDate());
                if (ObjectUtil.isNotNull(appHomeData.getHomeContentId())) {
                    appHomeDataService.updateById(appHomeData);
                } else {
                    appHomeData.setHomeParentId(homeParentId);
                    appHomeDataService.saveHsAppHomeData(appHomeData);
                }
            } catch (Exception e) {
                e.printStackTrace();
                throw new BaseException("app.home.config.save.error", new Object[]{appHomeData.toString()});
            }
        }
        for (HsAppHomeData homeData : this.removeList) {
            appHomeDataService.removeById(homeData.getHomeContentId());
        }
    }
}
