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

import com.mortals.xhx.module.dept.model.DeptEntity;
import com.mortals.xhx.module.dept.service.DeptService;
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.window.dao.WindowOwnerDao;
import com.mortals.xhx.module.window.model.WindowOwnerEntity;
import com.mortals.xhx.module.window.service.WindowOwnerService;
import org.springframework.beans.factory.annotation.Autowired;
import com.mortals.xhx.module.window.model.WindowOwnerDetailEntity;
import com.mortals.xhx.module.window.model.WindowOwnerDetailQuery;
import com.mortals.xhx.module.window.service.WindowOwnerDetailService;
import org.springframework.util.ObjectUtils;

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

import lombok.extern.slf4j.Slf4j;

/**
 * WindowOwnerService
 * 窗口负责人 service实现
 *
 * @author zxfei
 * @date 2023-07-12
 */
@Service("windowOwnerService")
@Slf4j
public class WindowOwnerServiceImpl extends AbstractCRUDServiceImpl<WindowOwnerDao, WindowOwnerEntity, Long> implements WindowOwnerService {
    @Autowired
    private WindowOwnerDetailService windowOwnerDetailService;
    @Autowired
    private DeptService deptService;


    @Override
    protected void saveBefore(WindowOwnerEntity entity, Context context) throws AppException {
        if (!ObjectUtils.isEmpty(entity.getWindowOwnerDetailList())) {
            entity.setWindowCount(entity.getWindowOwnerDetailList().size());
        }
        DeptEntity deptEntity = deptService.get(entity.getId());
        entity.setDeptName(deptEntity == null ? "" : deptEntity.getDeptName());
    }

    @Override
    protected void updateBefore(WindowOwnerEntity entity, Context context) throws AppException {
        if (!ObjectUtils.isEmpty(entity.getWindowOwnerDetailList())) {
            entity.setWindowCount(entity.getWindowOwnerDetailList().size());
        }
    }

    @Override
    protected void saveAfter(WindowOwnerEntity entity, Context context) throws AppException {
        if (!ObjectUtils.isEmpty(entity.getWindowOwnerDetailList())) {
            entity.getWindowOwnerDetailList().stream().peek(item -> {
                item.setOwnerId(entity.getId());
                item.setCreateUserId(this.getContextUserId(context));
                item.setCreateTime(new Date());
            }).count();


            windowOwnerDetailService.save(entity.getWindowOwnerDetailList());
        }
        super.saveAfter(entity, context);
    }



    @Override
    protected void updateAfter(WindowOwnerEntity entity, Context context) throws AppException {
        if (!ObjectUtils.isEmpty(entity.getWindowOwnerDetailList())) {
            Long[] windowOwnerDetailIds = windowOwnerDetailService.find(new WindowOwnerDetailQuery().ownerId(entity.getId())).stream().map(WindowOwnerDetailEntity::getId).toArray(Long[]::new);
            windowOwnerDetailService.remove(windowOwnerDetailIds, context);
            entity.getWindowOwnerDetailList().stream().peek(item -> {
                item.setOwnerId(entity.getId());
                item.setCreateUserId(this.getContextUserId(context));
                item.setCreateTime(new Date());
                item.setUpdateUserId(this.getContextUserId(context));
                item.setUpdateTime(new Date());
            }).count();
            entity.setNumber(entity.getWindowOwnerDetailList().size() + "");
            windowOwnerDetailService.save(entity.getWindowOwnerDetailList());
        }
        super.updateAfter(entity, context);
    }

    @Override
    protected void removeAfter(Long[] ids, Context context, int result) throws AppException {
        List<WindowOwnerDetailEntity> windowOwnerDetaillist = windowOwnerDetailService.find(new WindowOwnerDetailQuery().ownerIdList(Arrays.asList(ids)));
        windowOwnerDetailService.removeList(windowOwnerDetaillist, context);
        super.removeAfter(ids, context, result);
    }
}