package com.mortals.xhx.module.matter.service.impl;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.xhx.common.utils.BeanUtil;
import com.mortals.xhx.module.business.model.BusinessQuery;
import com.mortals.xhx.module.matter.model.MatterQuery;
import com.mortals.xhx.module.site.model.SiteMatterEntity;
import com.mortals.xhx.module.site.model.SiteBusinessQuery;
import com.mortals.xhx.module.site.model.SiteEntity;
import com.mortals.xhx.module.site.model.SiteMatterQuery;
import com.mortals.xhx.module.site.service.SiteBusinessService;
import com.mortals.xhx.module.site.service.SiteMatterService;
import com.mortals.xhx.module.site.service.SiteService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.xhx.module.matter.dao.MatterDao;
import com.mortals.xhx.module.matter.model.MatterEntity;
import com.mortals.xhx.module.matter.service.MatterService;
import org.springframework.util.ObjectUtils;

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

/**
* MatterService
* 基础事项 service实现
*
* @author zxfei
* @date 2022-01-12
*/
@Service("matterService")
public class MatterServiceImpl extends AbstractCRUDServiceImpl<MatterDao, MatterEntity, Long> implements MatterService {
    @Autowired
    private SiteMatterService siteMatterService;
    @Autowired
    private SiteService siteService;

    @Override
    protected void saveBefore(MatterEntity entity, Context context) throws AppException {

        super.saveBefore(entity, context);
    }

    @Override
    protected void updateBefore(MatterEntity entity, Context context) throws AppException {

        super.updateBefore(entity, context);
    }


    @Override
    protected void updateAfter(MatterEntity entity, Context context) throws AppException {
        //同步更新所有站点
        List<SiteMatterEntity> collect = siteMatterService.find(new SiteMatterQuery().matterId(entity.getId())).stream()
                .map(item -> {
                    item.setMatterName(entity.getMatterName());
                    return item;
                }).collect(Collectors.toList());
        siteMatterService.update(collect, context);
        super.updateAfter(entity, context);
    }


    @Override
    protected void removeBefore(Long[] ids, Context context) throws AppException {
        //有子节点 禁止删除

        super.removeBefore(ids, context);
    }


    @Override
    public void addMatterToSite(String matterIds, Long siteId, Context context) {

        if (ObjectUtils.isEmpty(siteId)) {
            throw new AppException("请选择对应站点");
        }
        List<Long> matterIdList = Arrays.asList(matterIds.split(",")).stream().map(Long::parseLong).collect(Collectors.toList());
        MatterQuery matterQuery = new MatterQuery();
        matterQuery.setIdList(matterIdList);
        this.find(matterQuery).stream()
                .peek(item -> updateOrSave(item, siteId, context)).count();
    }

    private void updateOrSave(MatterEntity item, Long siteId, Context context) {
        SiteMatterEntity siteMatterEntity = siteMatterService.selectOne(new SiteMatterQuery().siteId(siteId).matterId(item.getId()));
        if (!ObjectUtils.isEmpty(siteMatterEntity)) {
            BeanUtils.copyProperties(item, siteMatterEntity, new String[]{"id"});

            siteMatterEntity.setMatterName(item.getMatterName());
            siteMatterEntity.setUpdateTime(new Date());
            siteMatterService.update(siteMatterEntity, context);
        } else {
            siteMatterEntity = new SiteMatterEntity();
            SiteEntity siteEntity = siteService.get(siteId);
            siteMatterEntity.setSiteId(siteEntity.getId());
            siteMatterEntity.setSiteName(siteEntity.getSiteName());
            siteMatterEntity.setMatterId(item.getId());
            siteMatterEntity.setMatterName(item.getMatterName());
            siteMatterEntity.setCreateUserId(context.getUser().getId());
            siteMatterEntity.setCreateTime(new Date());
            siteMatterService.save(siteMatterEntity, context);
        }
    }
}