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

import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.xhx.base.system.upload.service.UploadService;
import com.mortals.xhx.module.matter.model.*;
import com.mortals.xhx.module.matter.service.MatterDatumFileService;
import com.mortals.xhx.module.matter.service.MatterService;
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.MatterDatumDao;
import com.mortals.xhx.module.matter.service.MatterDatumService;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * MatterDatumService
 * 事项申请材料 service实现
 *
 * @author zxfei
 * @date 2022-01-12
 */
@Service("matterDatumService")
public class MatterDatumServiceImpl extends AbstractCRUDServiceImpl<MatterDatumDao, MatterDatumEntity, Long> implements MatterDatumService {

    @Autowired
    private MatterService matterService;
    @Autowired
    private MatterDatumFileService matterDatumFileService;
    @Autowired
    private UploadService uploadService;


    @Override
    protected void findAfter(MatterDatumEntity params, Context context, List<MatterDatumEntity> list) throws AppException {

        list.stream().peek(item->{
            List<MatterDatumFileEntity> matterDatumFileEntities = matterDatumFileService.find(new MatterDatumFileQuery().datumId(item.getId()));
            item.setDatumFileList(matterDatumFileEntities);
        }).count();
        super.findAfter(params, context, list);
    }

    @Override
    protected void saveBefore(MatterDatumEntity entity, Context context) throws AppException {
        if (!ObjectUtils.isEmpty(entity.getMatterId())) {
            MatterEntity matterEntity = matterService.get(entity.getMatterId());
            entity.setMatterName(matterEntity==null?"":matterEntity.getMatterName());
        }
        super.saveBefore(entity, context);
    }


    @Override
    protected void saveAfter(MatterDatumEntity entity, Context context) throws AppException {
        if (!ObjectUtils.isEmpty(entity.getMatterDatumFileList())) {
            entity.getMatterDatumFileList().stream().peek(item -> {
                item.setDatumId(entity.getId());
            }).count();
            matterDatumFileService.save(entity.getMatterDatumFileList());
        }
        super.saveAfter(entity, context);
    }

    @Override
    protected void updateAfter(MatterDatumEntity entity, Context context) throws AppException {
        if (!ObjectUtils.isEmpty(entity.getMatterDatumFileList())) {
            //先删除后新增
            Long[] fileIds = matterDatumFileService.find(new MatterDatumFileQuery().datumId(entity.getId())).stream()
                    .map(item -> {
                        uploadService.deleteFile(item.getFileUrl());
                        return item.getId();
                    }).toArray(Long[]::new);
            matterDatumFileService.remove(fileIds, context);
            entity.getMatterDatumFileList().stream().peek(item -> {
                item.setDatumId(entity.getId());
            }).count();
            matterDatumFileService.save(entity.getMatterDatumFileList());
        }
        super.updateAfter(entity, context);
    }


}