package com.mortals.xhx.module.basic.service.impl;

import com.mortals.framework.ap.GlobalSysInfo;
import com.mortals.framework.model.PageInfo;
import com.mortals.xhx.common.key.Constant;
import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.xhx.module.basic.dao.BasicSetDao;
import com.mortals.xhx.module.basic.model.BasicSetEntity;
import com.mortals.xhx.module.basic.service.BasicSetService;
import org.springframework.beans.factory.annotation.Autowired;
import com.mortals.xhx.module.basic.model.BasicStatusEntity;
import com.mortals.xhx.module.basic.model.BasicStatusQuery;
import com.mortals.xhx.module.basic.service.BasicStatusService;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.Arrays;
import java.util.List;

import lombok.extern.slf4j.Slf4j;

/**
 * BasicSetService
 * 基础设置 service实现
 *
 * @author zxfei
 * @date 2023-05-23
 */
@Service("basicSetService")
@Slf4j
public class BasicSetServiceImpl extends AbstractCRUDServiceImpl<BasicSetDao, BasicSetEntity, Long> implements BasicSetService {
    @Autowired
    private BasicStatusService basicStatusService;

    @Override
    protected void findAfter(BasicSetEntity params, PageInfo pageInfo, Context context, List<BasicSetEntity> list) throws AppException {
        super.findAfter(params, pageInfo, context, list);

        String siteName = GlobalSysInfo.getParamValue(Constant.DEFAULT_SITENAME, "宜宾市民中心");
        list.forEach(item -> {
            item.setSiteName(siteName);
        });

    }

    @Override
    protected void saveAfter(BasicSetEntity entity, Context context) throws AppException {
        if (!ObjectUtils.isEmpty(entity.getBasicStatusList())) {
            entity.getBasicStatusList().stream().peek(item -> {
                item.setBasicSetId(entity.getId());
                item.setCreateUserId(this.getContextUserId(context));
                item.setCreateTime(new Date());
            }).count();
            basicStatusService.save(entity.getBasicStatusList());
        }
        super.saveAfter(entity, context);
    }

    @Override
    protected void updateAfter(BasicSetEntity entity, Context context) throws AppException {
        if (!ObjectUtils.isEmpty(entity.getBasicStatusList())) {
            Long[] basicStatusIds = basicStatusService.find(new BasicStatusQuery().basicSetId(entity.getId())).stream().map(BasicStatusEntity::getId).toArray(Long[]::new);
            basicStatusService.remove(basicStatusIds, context);
            entity.getBasicStatusList().stream().peek(item -> {
                item.setBasicSetId(entity.getId());
                item.setCreateUserId(this.getContextUserId(context));
                item.setCreateTime(new Date());
                item.setUpdateUserId(this.getContextUserId(context));
                item.setUpdateTime(new Date());
            }).count();
            basicStatusService.save(entity.getBasicStatusList());
        }
        super.updateAfter(entity, context);
    }

    @Override
    protected void removeAfter(Long[] ids, Context context, int result) throws AppException {
        List<BasicStatusEntity> basicStatuslist = basicStatusService.find(new BasicStatusQuery().basicSetIdList(Arrays.asList(ids)));
        basicStatusService.removeList(basicStatuslist, context);
        super.removeAfter(ids, context, result);
    }
}