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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.common.Rest;
import com.mortals.framework.model.OrderCol;
import com.mortals.xhx.base.system.user.service.UserService;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.common.pdu.RespData;
import com.mortals.xhx.common.pdu.window.WindowPdu;
import com.mortals.xhx.common.pdu.workman.WorkmanPdu;
import com.mortals.xhx.feign.window.IWindowFeign;
import com.mortals.xhx.feign.workman.IWorkmanFeign;
import com.mortals.xhx.module.staff.model.StaffEntity;
import com.mortals.xhx.module.staff.model.StaffQuery;
import com.mortals.xhx.module.staff.service.StaffService;
import com.mortals.xhx.module.window.model.*;
import com.mortals.xhx.module.window.service.WindowPerformService;
import com.mortals.xhx.module.window.service.WindowWorkmanPerformService;
import org.apache.commons.collections4.CollectionUtils;
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.service.WindowOwnerService;
import org.springframework.beans.factory.annotation.Autowired;
import com.mortals.xhx.module.window.service.WindowOwnerDetailService;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;

/**
 * WindowOwnerService
 * 窗口负责人 service实现
 *
 * @author zxfei
 * @date 2024-01-26
 */
@Service("windowOwnerService")
@Slf4j
public class WindowOwnerServiceImpl extends AbstractCRUDServiceImpl<WindowOwnerDao, WindowOwnerEntity, Long> implements WindowOwnerService {

    @Autowired
    private WindowOwnerDetailService windowOwnerDetailService;
    @Autowired
    private IWindowFeign windowFeign;
    @Autowired
    private StaffService staffService;
    @Autowired
    private WindowPerformService windowPerformService;
    @Autowired
    private WindowWorkmanPerformService windowWorkmanPerformService;

    @Autowired
    private UserService userService;
    @Autowired
    private IWorkmanFeign workmanFeign;


    @Override
    public List<WindowPdu> subWindowPduList(WindowPdu pdu) {
        List<WindowPdu> ownerList = new ArrayList<>();
        List<WindowPdu> subList = new ArrayList<>();
        List<WindowPdu> subTwoList = new ArrayList<>();
        List<WindowOwnerDetailEntity> ownerDetail = windowOwnerDetailService.getAllList();
        if (!CollectionUtils.isEmpty(ownerDetail)) {
            Long[] windows = ownerDetail.stream().map(WindowOwnerDetailEntity::getWindowId).toArray(Long[]::new);
            pdu.setIdNotList(Arrays.asList(windows));
            if (pdu.getSiteId() == null) {
                pdu.setSiteId(1l); //默认只查宜宾市民中心
            }
            pdu.setSize(-1);
            Rest<RespData<List<WindowPdu>>> respDataRest = windowFeign.list(pdu);
            log.info(JSONObject.toJSONString(respDataRest));
            if (respDataRest.getCode() == YesNoEnum.YES.getValue()) {
                subList = respDataRest.getData().getData();
            }

            if (!ObjectUtils.isEmpty(pdu.getStaffId())) {
                //查询当前负责人负责的窗口
                WindowOwnerDetailQuery detailQuery = new WindowOwnerDetailQuery();
                detailQuery.setOwnerId(pdu.getStaffId());
                Long[] windowIds = windowOwnerDetailService.find(detailQuery).stream().map(WindowOwnerDetailEntity::getWindowId).toArray(Long[]::new);
                if (!ObjectUtils.isEmpty(windowIds)) {
                    WindowPdu ownerWindowPdu = new WindowPdu();
                    ownerWindowPdu.setIdList(Arrays.asList(windowIds));
                    Rest<RespData<List<WindowPdu>>> ownerRest = windowFeign.list(ownerWindowPdu);
                    if (ownerRest.getCode() == YesNoEnum.YES.getValue()) {
                        ownerRest.getData().getData().stream().forEach(item -> {
                            item.setSelected(YesNoEnum.YES.getValue());
                        });
                        subTwoList = ownerRest.getData().getData();
                    }
                }
            }
        }
        ownerList.addAll(subList);
        ownerList.addAll(subTwoList);
        ownerList = ownerList.stream().sorted(Comparator.comparing(WindowPdu::getSelected).reversed()).collect(Collectors.toList());
        if (!ObjectUtils.isEmpty(pdu.getHallId())) {
            ownerList = ownerList.stream().filter(f -> pdu.getHallId().equals(f.getHallId())).collect(Collectors.toList());
        }


        return ownerList;
    }

    @Override
    public List<WindowOwnerDetailEntity> ownerWindowList(Context context) {
        Long customerId = context.getUser().getCustomerId();
        if (ObjectUtils.isEmpty(customerId)) throw new AppException("系统用户关联客户ID为空！");
        List<WindowOwnerDetailEntity> windowOwnerDetailEntities = windowOwnerDetailService.find(new WindowOwnerDetailQuery().ownerId(customerId));
        return windowOwnerDetailEntities;
    }

    @Override
    public Rest<List<StaffEntity>> getWindowPerson(Long windowId, Context context) {
        WorkmanPdu workmanPdu = new WorkmanPdu();
        workmanPdu.setWindowId(windowId);
        Rest<RespData<List<WorkmanPdu>>> rest = workmanFeign.list(workmanPdu);
        if (rest.getCode() == YesNoEnum.YES.getValue()) {
            List<StaffEntity> staffEntityList = rest.getData().getData().stream().map(item -> {
                //转换成staf
                String name = item.getName();
                String mobile = item.getMobile();
                if (!ObjectUtils.isEmpty(mobile)) {
                    StaffEntity staffEntity = staffService.selectOne(new StaffQuery().phoneNumber(mobile));
                    if (!ObjectUtils.isEmpty(staffEntity)) {
                        return staffEntity;
                    } else {

                    }
                    return getStaffEntityByName(name);
                } else if (!ObjectUtils.isEmpty(name)) {
                    //通过名字查找
                    return getStaffEntityByName(name);
                } else {
                    log.info("未找到对应员工:{}", JSON.toJSONString(item));
                    return null;
                }
            }).collect(Collectors.toList());
            return Rest.ok(staffEntityList);
        }
        return Rest.fail("获取窗口工作人员失败");
    }


    private StaffEntity getStaffEntityByName(String name) {
        StaffEntity staffEntity = staffService.selectOne(new StaffQuery().name(name));
        if (!ObjectUtils.isEmpty(staffEntity)) {
            return staffEntity;
        } else {
            return null;
        }
    }

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

    @Override
    protected void updateBefore(WindowOwnerEntity entity, Context context) throws AppException {
        super.updateBefore(entity, context);
        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.getStaffId());
                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.getStaffId())).stream().map(WindowOwnerDetailEntity::getId).toArray(Long[]::new);
            windowOwnerDetailService.remove(windowOwnerDetailIds, context);
            entity.getWindowOwnerDetailList().stream().peek(item -> {
                item.setOwnerId(entity.getStaffId());
                item.setCreateUserId(this.getContextUserId(context));
                item.setCreateTime(new Date());
                item.setUpdateUserId(this.getContextUserId(context));
                item.setUpdateTime(new Date());
            }).count();
            windowOwnerDetailService.save(entity.getWindowOwnerDetailList());
        }
        super.updateAfter(entity, context);
    }


    @Override
    public Rest<List<String>> getPerformMonthList(Context context) {
        Set<String> collectSet = new HashSet<>();
        //查询所有窗口汇总表
        WindowPerformQuery windowPerformQuery = new WindowPerformQuery();
        windowPerformQuery.setOwnerId(context.getUser().getCustomerId());
        windowPerformQuery.setOrderColList(Arrays.asList(new OrderCol("year", OrderCol.DESCENDING), new OrderCol("month", OrderCol.DESCENDING)));
        Map<String, List<WindowPerformEntity>> collect = windowPerformService.find(windowPerformQuery).stream().collect(Collectors.groupingBy(item -> item.getYear() + "-" + item.getMonth()));
        Set<String> performMonthSet = collect.keySet();
        collectSet.addAll(performMonthSet);

//查询所有窗口人员汇总表
        WindowWorkmanPerformQuery windowWorkmanPerformQuery = new WindowWorkmanPerformQuery();
        windowWorkmanPerformQuery.setOwnerId(context.getUser().getCustomerId());
        Map<String, List<WindowWorkmanPerformEntity>> workmanMonthSet = windowWorkmanPerformService.find(windowWorkmanPerformQuery).stream().collect(Collectors.groupingBy(item -> item.getYear() + "-" + item.getMonth()));
        collectSet.addAll(workmanMonthSet.keySet());

        //perform 排序 降序
        List<String> dateList = performMonthSet.stream().collect(Collectors.toList());
        Collections.sort(dateList, new Comparator<String>() {
            @Override
            public int compare(String object1, String object2) {
                return object1.compareTo(object2);
            }
        });

        return Rest.ok(dateList);
    }


    @Override
    protected void removeBefore(Long[] ids, Context context) throws AppException {
        super.removeBefore(ids, context);
        //删除窗口人员
        List<WindowOwnerEntity> windowOwnerEntities = this.get(ids, context);
        List<Long> collect = windowOwnerEntities.stream().map(item -> item.getWindowId()).collect(Collectors.toList());
        if (!ObjectUtils.isEmpty(collect)) {
            WindowOwnerDetailQuery detailQuery = new WindowOwnerDetailQuery();
            detailQuery.ownerIdList(collect);
            Long[] delIds = windowOwnerDetailService.find(detailQuery).stream().map(item -> item.getId()).toArray(Long[]::new);
            if (!ObjectUtils.isEmpty(delIds)) {
                windowOwnerDetailService.remove(delIds, context);
            }
        }

    }
}