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

import cn.hutool.core.util.StrUtil;
import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.model.PageInfo;
import com.mortals.framework.model.Result;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.xhx.base.framework.config.InterceptorConfig;
import com.mortals.xhx.common.code.SourceEnum;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.common.utils.BeanUtil;
import com.mortals.xhx.module.area.model.AreaEntity;
import com.mortals.xhx.module.area.service.AreaService;
import com.mortals.xhx.module.base.service.BaseAreaService;
import com.mortals.xhx.module.dept.model.DeptEntity;
import com.mortals.xhx.module.dept.service.DeptService;
import com.mortals.xhx.module.matter.dao.MatterDao;
import com.mortals.xhx.module.matter.model.MatterEntity;
import com.mortals.xhx.module.matter.model.MatterQuery;
import com.mortals.xhx.module.matter.model.vo.MatterInfo;
import com.mortals.xhx.module.matter.service.*;
import com.mortals.xhx.module.site.model.SiteEntity;
import com.mortals.xhx.module.site.model.SiteMatterEntity;
import com.mortals.xhx.module.site.model.SiteMatterQuery;
import com.mortals.xhx.module.site.service.SiteMatterService;
import com.mortals.xhx.module.site.service.SiteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
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")
@Slf4j
public class MatterServiceImpl extends AbstractCRUDServiceImpl<MatterDao, MatterEntity, Long> implements MatterService {
    @Autowired
    private SiteMatterService siteMatterService;
    @Autowired
    private SiteService siteService;

    @Autowired
    private InterceptorConfig interceptorConfig;

    @Autowired
    private MatterFlowlimitService matterFlowlimitService;
    @Autowired
    private MatterAcceptService matterAcceptService;
    @Autowired
    private MatterSetbaseService matterSetbaseService;
    @Autowired
    private MatterQuestionService matterQuestionService;
    @Autowired
    private MatterIntermediaryService matterIntermediaryService;
    @Autowired
    private MatterDatumService matterDatumService;
    @Autowired
    private MatterDatumFileService matterDatumFileService;
    @Autowired
    private MatterChargesService matterChargesService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private MatterExtService matterExtService;

    @Autowired
    private BaseAreaService baseAreaService;
    @Autowired
    private AreaService areaService;


    @Override
    protected void saveBefore(MatterEntity entity, Context context) throws AppException {
        //判断如果是自定义事项，需要更新区域编码


        DeptEntity extCache = deptService.getExtCache(entity.getDeptCode());
        entity.setDeptName(extCache == null ? "" : extCache.getName());

        if (!ObjectUtils.isEmpty(entity.getAreaCode())) {

            AreaEntity areaEntity = areaService.getExtCache(entity.getAreaCode());

            entity.setAreaName(areaEntity == null ? "" : areaEntity.getName());
            entity.setAreaLevel(areaEntity == null ? 0 : areaEntity.getAreaLevel());
        }

        super.saveBefore(entity, context);
    }

    /**
     * @param entity
     * @param context
     * @throws AppException
     */
    @Override
    protected void saveAfter(MatterEntity entity, Context context) throws AppException {

        super.saveAfter(entity, context);
    }

    @Override
    protected void updateBefore(MatterEntity entity, Context context) throws AppException {
        if (SourceEnum.自定义.getValue().equals(entity.getSource())) {
            if (!ObjectUtils.isEmpty(entity.getSiteId())) {
                SiteEntity siteEntity = siteService.get(entity.getSiteId(), context);
                entity.setAreaCode(siteEntity.getAreaCode());
                DeptEntity extCache = deptService.getExtCache(entity.getDeptCode());
                entity.setDeptName(extCache == null ? "" : extCache.getName());
            }
            MatterEntity beforeMatterEntity = this.get(entity.getId(), context);
            //如果部门切换了 级联修改关联的其它项
            if (!ObjectUtils.isEmpty(entity.getDeptCode())) {
                if (!entity.getDeptCode().equals(beforeMatterEntity.getDeptCode())) {
                    //更新站点事项关联项
                    SiteMatterQuery condition = new SiteMatterQuery();
                    condition.setMatterId(entity.getId());
                    SiteMatterQuery siteMatterQuery = new SiteMatterQuery();
                    DeptEntity extCache = deptService.getExtCache(entity.getDeptCode());

                    siteMatterQuery.setDeptId(extCache == null ? 0L : extCache.getId());
                    siteMatterQuery.setDeptCode(entity.getDeptCode());
                    siteMatterQuery.setDeptName(entity.getDeptName());
                    siteMatterService.updateBatch(siteMatterQuery, condition, context);

                }
            }

        }
        super.updateBefore(entity, context);
    }


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

        super.updateAfter(entity, context);
    }


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


    public static void main(String[] args) {

    }

}