package com.hw.ems.base.service.impl;

import com.hw.common.core.text.Convert;
import com.hw.common.core.utils.DateUtils;
import com.hw.common.datascope.annotation.DataScope;
import com.hw.common.security.utils.SecurityUtils;
import com.hw.ems.base.domain.BasePublicManage;
import com.hw.ems.base.domain.BasePublicUser;
import com.hw.ems.base.domain.BasePublicUserRecord;
import com.hw.ems.base.domain.vo.MonitorVo;
import com.hw.ems.base.mapper.BasePublicManageMapper;
import com.hw.ems.base.mapper.BasePublicUserRecordMapper;
import com.hw.ems.base.service.IBasePublicManageService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 公摊管理Service业务层处理
 *
 * @author ruoyi
 * @date 2023-04-27
 */
@Service
public class BasePublicManageServiceImpl implements IBasePublicManageService {
    @Autowired
    private BasePublicManageMapper basePublicManageMapper;

    @Autowired
    private BasePublicUserRecordMapper basePublicUserRecordMapper;

    /**
     * 查询公摊管理
     *
     * @param id 公摊管理主键
     * @return 公摊管理
     */
    @Override
    //@DS("#header.poolName")
    public BasePublicManage selectBasePublicManageById(Long id) {
        return basePublicManageMapper.selectBasePublicManageById(id);
    }

    /**
     * 查询公摊管理列表
     *
     * @param basePublicManage 公摊管理
     * @return 公摊管理
     */
    @Override
    @DataScope(deptAlias = "null")
    //@DS("#header.poolName")
    public List<BasePublicManage> selectBasePublicManageList(BasePublicManage basePublicManage) {
        return basePublicManageMapper.selectBasePublicManageList(basePublicManage);
    }

    /**
     * 新增公摊管理
     *
     * @param basePublicManage 公摊管理
     * @return 结果
     */
    @Override
    //@DS("#header.poolName")
    public int insertBasePublicManage(BasePublicManage basePublicManage) {
        LocalDateTime currentTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String nowDate = currentTime.format(formatter);
        basePublicManage.setId(Long.parseLong(nowDate));
        basePublicManage.setCreateTime(DateUtils.getNowDate());
        this.insertPublicUserRecord(basePublicManage);
        return basePublicManageMapper.insertBasePublicManage(basePublicManage);
    }

    /**
     * 添加公摊用户数据记录
     * @param basePublicManage
     */
    //@DS("#header.poolName")
    private void insertPublicUserRecord(BasePublicManage basePublicManage) {
        List<BasePublicUser> basePublicUsers = basePublicManage.getBasePublicUsers();
        try {
            for (BasePublicUser basePublicUser : basePublicUsers) {
                BasePublicUserRecord publicUserRecord = new BasePublicUserRecord();
                publicUserRecord.setPublicShareId(String.valueOf(basePublicManage.getId()));
                publicUserRecord.setPublicUserId(basePublicUser.getId());
                List<String> monitorIdList = basePublicUser.getMonitors().stream().filter(e -> e.getPublicShareType() == 0)
                        .map(MonitorVo::getMonitorId).collect(Collectors.toList());
                publicUserRecord.setPublicMonitorIds(String.join(",", monitorIdList));
                publicUserRecord.setStartTime(basePublicManage.getBeginTime());
                publicUserRecord.setEndTime(basePublicManage.getEndTime());
                publicUserRecord.setAllocationRatio(basePublicUser.getPercentage());
                publicUserRecord.setPublicEnergyConsumption(basePublicUser.getPublicExpend());
                publicUserRecord.setOwnerEnergyConsumption(basePublicUser.getUserExpend());
                publicUserRecord.setEnergyType(Long.parseLong(basePublicManage.getPublicShareType()));
                publicUserRecord.setCreatedBy(SecurityUtils.getUsername());
                publicUserRecord.setCreatedTime(DateUtils.getNowDate());
                basePublicUserRecordMapper.insertBasePublicUserRecord(publicUserRecord);
            }
        } catch (NumberFormatException e) {
            System.out.println("添加公摊用户数据记录异常:" + e.getMessage());
        }
    }

    /**
     * 修改公摊管理
     *
     * @param basePublicManage 公摊管理
     * @return 结果
     */
    @Override
    //@DS("#header.poolName")
    public int updateBasePublicManage(BasePublicManage basePublicManage) {
        basePublicManage.setUpdateTime(DateUtils.getNowDate());
        return basePublicManageMapper.updateBasePublicManage(basePublicManage);
    }

    /**
     * 批量删除公摊管理
     *
     * @param ids 需要删除的公摊管理主键
     * @return 结果
     */
    @Override
    //@DS("#header.poolName")
    public int deleteBasePublicManageByIds(String ids) {
        return basePublicManageMapper.deleteBasePublicManageByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除公摊管理信息
     *
     * @param id 公摊管理主键
     * @return 结果
     */
    @Override
    //@DS("#header.poolName")
    public int deleteBasePublicManageById(Long id) {
        return basePublicManageMapper.deleteBasePublicManageById(id);
    }

    /**
     * 第二步：确认公摊
     *
     * @param basePublicManage
     * @return
     */
    @Override
    //@DS("#header.poolName")
    public List<BasePublicManage> AddStepTwo(BasePublicManage basePublicManage) {
        List<BasePublicManage> basePublicManages = new ArrayList<>();


        return null;
    }


}
