package com.ruoyi.cs.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.cs.domain.CsExpireRemind;
import com.ruoyi.cs.domain.CsResourceCharging;
import com.ruoyi.cs.mapper.CsExpireRemindMapper;
import com.ruoyi.cs.mapper.CsResourceChargingMapper;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.service.ISysNoticeService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cs.mapper.CsPricingInventoryMapper;
import com.ruoyi.cs.domain.CsPricingInventory;
import com.ruoyi.cs.service.ICsPricingInventoryService;

import javax.annotation.Resource;

/**
 * 计价清单Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-07
 */
@Service
public class CsPricingInventoryServiceImpl implements ICsPricingInventoryService
{
    @Resource
    private CsPricingInventoryMapper csPricingInventoryMapper;
    @Resource
    private CsResourceChargingMapper csResourceChargingMapper;
    @Resource
    private CsExpireRemindMapper csExpireRemindMapper;

    @Resource
    private ISysNoticeService sysNoticeService;

    /**
     * 查询计价清单
     *
     * @param inventoryId 计价清单主键
     * @return 计价清单
     */
    @Override
    public CsPricingInventory selectCsPricingInventoryByInventoryId(String inventoryId)
    {
        CsPricingInventory csPricingInventory = csPricingInventoryMapper.selectCsPricingInventoryByInventoryId(inventoryId);
        if (ObjectUtils.isNotEmpty(csPricingInventory)) {
            CsResourceCharging csResourceCharging = new CsResourceCharging();
            csResourceCharging.setResId(csPricingInventory.getInventoryId());
            csResourceCharging.setDelFlag("0");
            csPricingInventory.setResourceChargingList(csResourceChargingMapper.selectCsResourceChargingList(csResourceCharging));

            CsExpireRemind csExpireRemind = new CsExpireRemind();
            csExpireRemind.setResId(csPricingInventory.getInventoryId());
            csExpireRemind.setDelFlag("0");
            csPricingInventory.setExpireRemindList(csExpireRemindMapper.selectCsExpireRemindList(csExpireRemind));
        }
        return csPricingInventory;
    }

    /**
     * 查询计价清单列表
     *
     * @param csPricingInventory 计价清单
     * @return 计价清单
     */
    @Override
    public List<CsPricingInventory> selectCsPricingInventoryList(CsPricingInventory csPricingInventory)
    {
        return csPricingInventoryMapper.selectCsPricingInventoryList(csPricingInventory);
    }

    /**
     * 新增计价清单
     *
     * @param csPricingInventory 计价清单
     * @return 结果
     */
    @Override
    public int insertCsPricingInventory(CsPricingInventory csPricingInventory)
    {
        String inventoryId = IdUtil.getSnowflakeNextIdStr();
        csPricingInventory.setInventoryId(inventoryId);
        csPricingInventory.setCreateAt(DateUtils.getNowDate());
        csPricingInventory.setDelFlag("0");
        int i = csPricingInventoryMapper.insertCsPricingInventory(csPricingInventory);

        // 资源计费说明
        List<CsResourceCharging> resourceChargingList = csPricingInventory.getResourceChargingList();
        if (resourceChargingList.size() > 0) {
            for (CsResourceCharging resourceCharging : resourceChargingList) {
                resourceCharging.setResourceChargingId(IdUtil.getSnowflakeNextIdStr());
                resourceCharging.setResId(inventoryId);
                resourceCharging.setResourceClassId(resourceCharging.getResourceClassId());
                resourceCharging.setCreateAt(DateUtils.getNowDate());
                resourceCharging.setDelFlag("0");
            }
            csResourceChargingMapper.bachInsertCsResourceCharging(resourceChargingList);
        }

        List<CsExpireRemind> expireRemindList = csPricingInventory.getExpireRemindList();
        if (expireRemindList.size() > 0) {
            for (CsExpireRemind csExpireRemind : expireRemindList) {
                csExpireRemind.setRemindId(IdUtil.getSnowflakeNextIdStr());
                csExpireRemind.setResId(inventoryId);
                csExpireRemind.setCreateAt(DateUtils.getNowDate());
                csExpireRemind.setDelFlag("0");
            }
            csExpireRemindMapper.bachInsertCsExpireRemind(expireRemindList);
        }
        return i;
    }

    /**
     * 修改计价清单
     *
     * @param csPricingInventory 计价清单
     * @return 结果
     */
    @Override
    public int updateCsPricingInventory(CsPricingInventory csPricingInventory)
    {
        csPricingInventory.setUpdateAt(DateUtils.getNowDate());
        int i = csPricingInventoryMapper.updateCsPricingInventory(csPricingInventory);

        csResourceChargingMapper.deleteCsResourceChargingByResId(csPricingInventory.getInventoryId());
        // 资源计费说明
        List<CsResourceCharging> resourceChargingList = csPricingInventory.getResourceChargingList();
        if (resourceChargingList.size() > 0) {
            for (CsResourceCharging resourceCharging : resourceChargingList) {
                resourceCharging.setResourceChargingId(IdUtil.getSnowflakeNextIdStr());
                resourceCharging.setResId(csPricingInventory.getInventoryId());
                resourceCharging.setResourceClassId(resourceCharging.getResourceClassId());
                resourceCharging.setCreateAt(DateUtils.getNowDate());
                resourceCharging.setDelFlag("0");
            }
            csResourceChargingMapper.bachInsertCsResourceCharging(resourceChargingList);
        }

        csExpireRemindMapper.deleteCsExpireRemindByResId(csPricingInventory.getInventoryId());
        List<CsExpireRemind> expireRemindList = csPricingInventory.getExpireRemindList();
        if (expireRemindList.size() > 0) {
            for (CsExpireRemind csExpireRemind : expireRemindList) {
                csExpireRemind.setRemindId(IdUtil.getSnowflakeNextIdStr());
                csExpireRemind.setResId(csPricingInventory.getInventoryId());
                csExpireRemind.setCreateAt(DateUtils.getNowDate());
                csExpireRemind.setDelFlag("0");
            }
            csExpireRemindMapper.bachInsertCsExpireRemind(expireRemindList);
        }
        return i;
    }

    /**
     * 批量删除计价清单
     *
     * @param inventoryIds 需要删除的计价清单主键
     * @return 结果
     */
    @Override
    public int deleteCsPricingInventoryByInventoryIds(String[] inventoryIds)
    {
        return csPricingInventoryMapper.deleteCsPricingInventoryByInventoryIds(inventoryIds);
    }

    /**
     * 删除计价清单信息
     *
     * @param inventoryId 计价清单主键
     * @return 结果
     */
    @Override
    public int deleteCsPricingInventoryByInventoryId(String inventoryId)
    {
        return csPricingInventoryMapper.deleteCsPricingInventoryByInventoryId(inventoryId);
    }

    /**
     * 查询计价清单列表
     *
     * @param inventoryIds 计价清单主键ID
     * @return 计价清单集合
     */
    @Override
    public List<CsPricingInventory> selectCsPricingInventoryInventoryIds(String[] inventoryIds) {
        return csPricingInventoryMapper.selectCsPricingInventoryInventoryIds(inventoryIds);
    }

    /**
     * 统计待修改已过期的计价清单列表
     * @return
     */
    @Override
    public int countAccountExpirationList() {
        return csPricingInventoryMapper.countAccountExpirationList();
    }

    /**
     * 修改已过期的计价清单列表
     * @return
     */
    @Override
    public int editAccountExpirationList() {
        //通知
        List<CsPricingInventory> needNoticeList = csPricingInventoryMapper.selectNeedNoticeAccountExpirationList();
        if(!CollectionUtils.isEmpty(needNoticeList)) {
            SysNotice model = sysNoticeService.selectNoticeById(2L);
            String content = model.getNoticeContent();
            List<SysNotice> noticeList = new ArrayList<>();
            for (CsPricingInventory node : needNoticeList) {
                SysNotice a = new SysNotice();
                a.setMemberUserId(node.getMemberUserId());
                a.setResId(node.getInventoryId());
                a.setNoticeTitle("账号到期");
                a.setNoticeContent(content);
                a.setNoticeType("2");
                a.setStatus("0");
                a.setCreateBy("定时任务");
                noticeList.add(a);
            }
            if(!CollectionUtils.isEmpty(noticeList)){
                sysNoticeService.batchInsertNotices(noticeList);
            }
        }
        //修改
        return csPricingInventoryMapper.editAccountExpirationList();
    }

    /**
     * 获取计价清单_资源计费说明列表
     * @param inventoryId 计价单id
     * @return
     */
    @Override
    public List<CsResourceCharging> selectResourceChargingListByinventoryId(String inventoryId) {
        CsPricingInventory csPricingInventory = csPricingInventoryMapper.selectCsPricingInventoryByInventoryId(inventoryId);
        if (ObjectUtils.isNotEmpty(csPricingInventory)) {
            CsResourceCharging csResourceCharging = new CsResourceCharging();
            csResourceCharging.setResId(csPricingInventory.getInventoryId());
            csResourceCharging.setDelFlag("0");
            return csResourceChargingMapper.selectCsResourceChargingList(csResourceCharging);
        }
        return null;
    }
}
