package org.springblade.modules.investment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.investment.mapper.IbaProjectFundMapper;
import org.springblade.modules.investment.mapper.IbaProjectVolumeMapper;
import org.springblade.modules.investment.pojo.dto.IbaProjectVolumeDTO;
import org.springblade.modules.investment.pojo.entity.IbaProject;
import org.springblade.modules.investment.mapper.IbaProjectMapper;
import org.springblade.modules.investment.pojo.entity.IbaProjectFund;
import org.springblade.modules.investment.pojo.entity.IbaProjectVolume;
import org.springblade.modules.investment.pojo.query.IbaProjectIncomeQuery;
import org.springblade.modules.investment.pojo.query.IbaProjectQuery;
import org.springblade.modules.investment.pojo.query.IbaProjectVolumeQuery;
import org.springblade.modules.investment.pojo.vo.IbaProjectCompanyTotalVo;
import org.springblade.modules.investment.pojo.vo.IbaProjectTotalVo;
import org.springblade.modules.investment.pojo.vo.IbaProjectTypeTotalVo;
import org.springblade.modules.investment.pojo.vo.incomeTotalVo;
import org.springblade.modules.investment.service.IbaProjectFundService;
import org.springblade.modules.investment.service.IbaProjectService;
import org.springblade.modules.investment.service.IbaProjectVolumeService;
import org.springblade.modules.project.utils.SnowFlakeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

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

/**
 * @author LHB
 * @description 针对表【iba_project(投资效益分析-项目表)】的数据库操作Service实现
 * @createDate 2025-05-06 09:59:53
 */
@Service
public class IbaProjectServiceImpl extends ServiceImpl<IbaProjectMapper, IbaProject>
        implements IbaProjectService {

    @Autowired
    private IbaProjectVolumeService projectVolumeService;
    @Autowired
    private IbaProjectVolumeMapper projectVolumeMapper;
    @Autowired
    private IbaProjectFundService projectFundService;
    @Autowired
    private IbaProjectFundMapper projectFundMapper;

    @Override
    public IbaProject queryById(Long id) {
        IbaProject ibaProject = baseMapper.selectById(id);
        if (ibaProject != null) {
            List<IbaProjectFund> ibaProjectFunds = projectFundMapper.selectMyList(id);
            //资金信息
            ibaProject.setFunds(ibaProjectFunds);

            //车流量信息
            List<IbaProjectVolumeDTO> ibaProjectVolumeDTO = projectVolumeMapper.selectGroupYearList(id);
            ibaProject.setList(ibaProjectVolumeDTO);


        }
        return ibaProject;
    }

    @Override
    public IPage<IbaProject> queryByPage(IbaProjectQuery query) {
        Page<IbaProject> page = new Page<>(query.getCurrent(), query.getSize());
        IPage<IbaProject> ibaProjectIPage = baseMapper.selectMyPage(page, query);
        return ibaProjectIPage;
    }

    @Override
    @Transactional
    public boolean insert(IbaProject ibaProject) {
        Long name = baseMapper.selectCount(new QueryWrapper<IbaProject>().lambda()
                .eq(IbaProject::getProjectName, ibaProject.getProjectName())
                .eq(IbaProject::getIsDeleted, 0));
        if (name > 0) {
            throw new ServiceException("项目名已存在");
        }

        ibaProject.setId(SnowFlakeUtil.getId());
        baseMapper.insert(ibaProject);
        //添加车流量信息
        if (!CollectionUtils.isEmpty(ibaProject.getList())) {
            List<IbaProjectVolume> ibaProjectVolumes = new ArrayList<>();


            for (IbaProjectVolumeDTO ibaProjectVolumeDTO : ibaProject.getList()) {

                String year = ibaProjectVolumeDTO.getYear();
                List<Integer> numbers = ibaProjectVolumeDTO.getNumbers();

                for (int i = 0; i < numbers.size(); i++) {
                    IbaProjectVolume ibaProjectVolume = new IbaProjectVolume();
                    ibaProjectVolume.setId(SnowFlakeUtil.getId());
                    ibaProjectVolume.setProjectId(ibaProject.getId());
                    ibaProjectVolume.setYear(year);
                    ibaProjectVolume.setMonth(i + 1);
                    ibaProjectVolume.setNumber(numbers.get(i));
                    ibaProjectVolumes.add(ibaProjectVolume);
                }
            }

            //批量新增
            projectVolumeService.saveBatch(ibaProjectVolumes);
        }
        //添加资金信息
        if (!CollectionUtils.isEmpty(ibaProject.getFunds())) {
            for (IbaProjectFund fund : ibaProject.getFunds()) {
                fund.setId(SnowFlakeUtil.getId());
                fund.setProjectId(ibaProject.getId());
            }
            projectFundService.saveBatch(ibaProject.getFunds());
        }

        return true;
    }

    @Override
    @Transactional
    public boolean update(IbaProject ibaProject) {
        Long name = baseMapper.selectCount(new QueryWrapper<IbaProject>().lambda()
                .eq(IbaProject::getProjectName, ibaProject.getProjectName())
                .ne(IbaProject::getId, ibaProject.getId())
                .eq(IbaProject::getIsDeleted, 0));
        if (name > 0) {
            throw new ServiceException("项目名已存在");
        }

        baseMapper.updateById(ibaProject);
        //删除旧数据
        projectVolumeMapper.delete(new QueryWrapper<IbaProjectVolume>().lambda().eq(IbaProjectVolume::getProjectId, ibaProject.getId()));
        //删除资金信息
        projectFundMapper.delete(new QueryWrapper<IbaProjectFund>().lambda().eq(IbaProjectFund::getProjectId, ibaProject.getId()));
        //添加新数据
        //添加车流量信息
        if (!CollectionUtils.isEmpty(ibaProject.getList())) {
            List<IbaProjectVolume> ibaProjectVolumes = new ArrayList<>();


            for (IbaProjectVolumeDTO ibaProjectVolumeDTO : ibaProject.getList()) {

                String year = ibaProjectVolumeDTO.getYear();
                List<Integer> numbers = ibaProjectVolumeDTO.getNumbers();

                for (int i = 0; i < numbers.size(); i++) {
                    IbaProjectVolume ibaProjectVolume = new IbaProjectVolume();
                    ibaProjectVolume.setId(SnowFlakeUtil.getId());
                    ibaProjectVolume.setProjectId(ibaProject.getId());
                    ibaProjectVolume.setYear(year);
                    ibaProjectVolume.setMonth(i + 1);
                    ibaProjectVolume.setNumber(numbers.get(i));
                    ibaProjectVolumes.add(ibaProjectVolume);
                }
            }

            //批量新增
            projectVolumeService.saveBatch(ibaProjectVolumes);
        }
        //添加资金信息
        if (!CollectionUtils.isEmpty(ibaProject.getFunds())) {
            for (IbaProjectFund fund : ibaProject.getFunds()) {
                fund.setId(SnowFlakeUtil.getId());
                fund.setProjectId(ibaProject.getId());
            }
            projectFundService.saveBatch(ibaProject.getFunds());
        }

        return true;
    }

    @Override
    @Transactional
    public boolean deleteById(String ids) {
        baseMapper.updateIsDeleted(Func.toLongList(ids), 1);
        //删除旧数据
        projectVolumeMapper.delete(new QueryWrapper<IbaProjectVolume>().lambda().in(IbaProjectVolume::getProjectId, Func.toLongList(ids)));
        //删除资金信息
        projectFundMapper.delete(new QueryWrapper<IbaProjectFund>().lambda().in(IbaProjectFund::getProjectId, Func.toLongList(ids)));
        return true;
    }

    @Override
    public IbaProjectTotalVo total() {
        //查询项目总数
        Long projectCount = baseMapper.selectCount(new QueryWrapper<IbaProject>().lambda().eq(IbaProject::getIsDeleted, 0));
        //查询公司总数
        Long companyTotal = baseMapper.companyTotal();

        IbaProjectTotalVo ibaProjectTotalVo = new IbaProjectTotalVo();
        ibaProjectTotalVo.setProjectNum(projectCount);
        ibaProjectTotalVo.setCompanyNum(companyTotal);

        return ibaProjectTotalVo;
    }

    @Override
    public List<IbaProjectTypeTotalVo> typeTotal() {
        return baseMapper.typeTotal();
    }

    @Override
    public List<IbaProjectCompanyTotalVo> companyTotal() {
        return baseMapper.companyGroupTotal();
    }

    @Override
    public List<incomeTotalVo> incomeTotalList(IbaProjectIncomeQuery query) {
        if (query.getType() == null || (query.getType() != 1 && query.getType() != 2)) {
            query.setType(1);
        }
        return baseMapper.incomeTotalList(query);
    }

    @Override
    public List<IbaProjectVolume> companyList(IbaProjectVolumeQuery query) {
        List<IbaProjectVolume> list = baseMapper.companyList(query);
        return list;
    }

    @Override
    public List<IbaProject> selectProjectOrCompanyList(Integer type) {
        QueryWrapper<IbaProject> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().select(IbaProject::getId, IbaProject::getProjectName, IbaProject::getOperationCompany)
                .eq(IbaProject::getIsDeleted, 0)
                .groupBy(type == 1 ? IbaProject::getId : IbaProject::getOperationCompany);
        return baseMapper.selectList(queryWrapper);
    }
}




