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

import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.model.PageInfo;
import com.mortals.xhx.common.utils.BeanUtil;
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.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.window.model.WindowBusinessEntity;
import com.mortals.xhx.module.window.model.WindowBusinessQuery;
import com.mortals.xhx.module.window.service.WindowBusinessService;
import com.mortals.xhx.module.workman.model.WorkmanEntity;
import com.mortals.xhx.module.workman.model.WorkmanQuery;
import com.mortals.xhx.module.workman.service.WorkmanService;
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.window.dao.WindowDao;
import com.mortals.xhx.module.window.model.WindowEntity;
import com.mortals.xhx.module.window.service.WindowService;
import org.springframework.util.ObjectUtils;

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

/**
 * WindowService
 * 站点部门窗口 service实现
 *
 * @author zxfei
 * @date 2022-01-12
 */
@Service("windowService")
public class WindowServiceImpl extends AbstractCRUDServiceImpl<WindowDao, WindowEntity, Long> implements WindowService {

    @Autowired
    private WorkmanService workmanService;

    @Autowired
    private WindowBusinessService windowBusinessService;
    @Autowired
    private BusinessService businessService;


    @Override
    protected void findAfter(WindowEntity params, PageInfo pageInfo, Context context, List<WindowEntity> list) throws AppException {
        Map<Long, List<WorkmanEntity>> collect = workmanService.find(new WorkmanQuery()).stream().filter(f->f.getWindowId()!=null).collect(Collectors.groupingBy(x -> x.getWindowId()));
        list.stream().peek(item -> {
            if (!ObjectUtils.isEmpty(collect.get(item.getId()))) {
                item.setWorkmanList(collect.get(item.getId()));
            }
        }).count();
        super.findAfter(params, pageInfo, context, list);
    }


    @Override
    public void addBusinessToWindow(String businessIds, Long windowId, Context context) {
        if (ObjectUtils.isEmpty(windowId)) {
            throw new AppException("请选择对应窗口");
        }
        List<Long> businessIdList = Arrays.asList(businessIds.split(",")).stream().map(Long::parseLong).collect(Collectors.toList());
        WindowBusinessQuery windowBusinessQuery = new WindowBusinessQuery();
        //windowBusinessQuery.setSiteBusinessIdList(businessIdList);

        //先删除后再新增
        windowBusinessQuery.setWindowId(windowId);
        Long[] ids = windowBusinessService.find(windowBusinessQuery).stream()
                .map(WindowBusinessEntity::getId).toArray(Long[]::new);
        if (!ObjectUtils.isEmpty(ids)) {
            windowBusinessService.remove(ids, context);
        }

        WindowEntity windowEntity = this.get(windowId, context);

        List<WindowBusinessEntity> windowBusinessEntities = businessIdList.stream().map(id -> {
            WindowBusinessEntity windowBusinessEntity = new WindowBusinessEntity();
            windowBusinessEntity.initAttrValue();
            if (!ObjectUtils.isEmpty(windowEntity)) {
                windowBusinessEntity.setWindowId(windowId);
                windowBusinessEntity.setWindowName(windowEntity.getName());
            } else {
                return null;
            }
            BusinessEntity businessEntity = businessService.get(id, context);
            if (!ObjectUtils.isEmpty(businessEntity)) {
                windowBusinessEntity.setSiteBusinessId(id);
                windowBusinessEntity.setBusinessName(businessEntity.getName());
            } else {
                return null;
            }
            return windowBusinessEntity;
        }).collect(Collectors.toList());
        windowBusinessService.save(windowBusinessEntities, context);
    }

}