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

import cn.hutool.core.util.StrUtil;
import com.mortals.framework.model.PageInfo;
import com.mortals.xhx.common.code.IsBusinessEnum;
import com.mortals.xhx.common.utils.BeanUtil;
import com.mortals.xhx.module.site.model.SiteBusinessEntity;
import com.mortals.xhx.module.site.model.SiteBusinessQuery;
import com.mortals.xhx.module.site.model.SiteEntity;
import com.mortals.xhx.module.site.service.SiteBusinessService;
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.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.xhx.common.code.SatusEnum;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.xhx.module.business.dao.BusinessDao;
import com.mortals.xhx.module.business.model.BusinessEntity;
import com.mortals.xhx.module.business.model.BusinessQuery;
import com.mortals.xhx.module.business.service.BusinessService;
import com.mortals.xhx.module.business.model.BusinessTreeSelect;
import org.springframework.util.ObjectUtils;

import java.util.*;
import java.util.stream.Collectors;

import static com.mortals.framework.ap.SysConstains.RESULT_KEY;

/**
 * BusinessService
 * 站点业务 service实现
 *
 * @author zxfei
 * @date 2022-01-13
 */
@Service("businessService")
public class BusinessServiceImpl extends AbstractCRUDServiceImpl<BusinessDao, BusinessEntity, Long> implements BusinessService {
    @Autowired
    private SiteBusinessService siteBusinessService;
    @Autowired
    private SiteService siteService;


    @Override
    protected void findAfter(BusinessEntity params, PageInfo pageInfo, Context context, List<BusinessEntity> list) throws AppException {
            list.stream().peek(item->{
                List<BusinessEntity> childs = this.find(new BusinessQuery().parentId(item.getId()));
                if(!ObjectUtils.isEmpty(childs)){
                    item.setChildren(childs);
                }else{
                    item.setChildren(null);
                }
            }).count();
        super.findAfter(params, pageInfo, context, list);
    }

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

        super.saveBefore(entity, context);
    }

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

        super.updateBefore(entity, context);
    }


    @Override
    protected void updateAfter(BusinessEntity entity, Context context) throws AppException {
        //同步更新所有站点
        List<SiteBusinessEntity> collect = siteBusinessService.find(new SiteBusinessQuery().businessId(entity.getId())).stream()
                .map(item -> {
                    BeanUtils.copyProperties(entity, item, new String[]{"id"});
                    item.setBusinessName(entity.getName());
                    return item;
                }).collect(Collectors.toList());
        siteBusinessService.update(collect, context);

        super.updateAfter(entity, context);
    }


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

        super.removeBefore(ids, context);
    }


    @Override
    public void addBusinessToSite(String businessIds, Long siteId, Context context) {

        if (ObjectUtils.isEmpty(siteId)) {
            throw new AppException("请选择对应站点");
        }
        List<Long> businessIdList = Arrays.asList(businessIds.split(",")).stream().map(Long::parseLong).collect(Collectors.toList());
        BusinessQuery businessQuery = new BusinessQuery();
        businessQuery.setIdList(businessIdList);
        this.find(businessQuery).stream()
                .map(item -> {
                    //更新或新增
                    updateOrSave(item, siteId, context);
                    return item;
                })
                .flatMap(item -> this.find(new BusinessQuery().parentId(item.getId())).stream())
                .peek(item -> {
                    //更新或新增
                    updateOrSave(item, siteId, context);
                }).count();
    }

    private void updateOrSave(BusinessEntity item, Long siteId, Context context) {
        SiteBusinessEntity siteBusinessEntity = siteBusinessService.selectOne(new SiteBusinessQuery().siteId(siteId).businessId(item.getId()));
        //SiteBusinessEntity siteBusinessEntity = collect.get(item.getId());
        if (!ObjectUtils.isEmpty(siteBusinessEntity)) {

            BeanUtils.copyProperties(item, siteBusinessEntity, new String[]{"id"});

            siteBusinessEntity.setBusinessName(item.getName());
            siteBusinessEntity.setUpdateTime(new Date());
            siteBusinessService.update(siteBusinessEntity, context);
        } else {
            siteBusinessEntity = new SiteBusinessEntity();
            BeanUtils.copyProperties(item, siteBusinessEntity, BeanUtil.getNullPropertyNames(item));
            SiteEntity siteEntity = siteService.get(siteId);
            siteBusinessEntity.setSiteId(siteEntity.getId());
            siteBusinessEntity.setSiteName(siteEntity.getSiteName());
            siteBusinessEntity.setBusinessId(item.getId());
            siteBusinessEntity.setBusinessName(item.getName());
            siteBusinessEntity.setId(null);
            siteBusinessEntity.setCreateUserId(context.getUser().getId());
            siteBusinessEntity.setCreateTime(new Date());
            siteBusinessService.save(siteBusinessEntity, context);
        }
    }
}