package com.fjhx.account.service.account.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fjhx.account.entity.account.po.AccountDeptRunningWater;
import com.fjhx.account.entity.account.po.AccountDeptRunningWaterDetail;
import com.fjhx.account.entity.account.po.AccountRunningWater;
import com.fjhx.account.entity.account.vo.AccountRunningWaterVo;
import com.fjhx.account.mapper.account.AccountDeptRunningWaterDetailMapper;
import com.fjhx.account.service.account.AccountDeptRunningWaterDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.common.constant.SourceConstant;
import com.fjhx.account.service.account.AccountDeptRunningWaterService;
import com.fjhx.account.service.account.AccountRunningWaterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjhx.account.entity.account.vo.AccountDeptRunningWaterDetailVo;
import com.fjhx.account.entity.account.dto.AccountDeptRunningWaterDetailSelectDto;
import com.ruoyi.common.utils.wrapper.IWrapper;
import com.fjhx.account.entity.account.dto.AccountDeptRunningWaterDetailDto;
import cn.hutool.core.bean.BeanUtil;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 资金流水分拆部门表 服务实现类
 * </p>
 *
 * @author
 * @since 2023-04-13
 */
@Service
public class AccountDeptRunningWaterDetailServiceImpl extends ServiceImpl<AccountDeptRunningWaterDetailMapper, AccountDeptRunningWaterDetail> implements AccountDeptRunningWaterDetailService {
    @Autowired
    private AccountRunningWaterService accountRunningWaterService;

    @Autowired
    private AccountDeptRunningWaterService accountDeptRunningWaterService;

    @Override
    public Page<AccountDeptRunningWaterDetailVo> getPage(AccountDeptRunningWaterDetailSelectDto dto) {
        IWrapper<AccountDeptRunningWaterDetail> wrapper = getWrapper();
        wrapper.orderByDesc("adrwd", AccountDeptRunningWaterDetail::getId);
        Page<AccountDeptRunningWaterDetailVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        return page;
    }

    /**
     * 资金流水分拆部门表明细
     */
    @Override
    public AccountRunningWaterVo detail(Long id) {
        //查询资金流水信息
        AccountRunningWater accountRunningWater = accountRunningWaterService.getById(id);
        AccountRunningWaterVo accountRunningWaterVo = BeanUtil.toBean(accountRunningWater, AccountRunningWaterVo.class);
            //判断是否有添加部门-资金流水表的信息
        if (ObjectUtil.isNotEmpty(accountRunningWaterVo)){
            //查询资金流水-部门关联表的信息(一个流水对应一个部门)
            AccountDeptRunningWater accountDeptRunningWater = accountDeptRunningWaterService.getOne(Wrappers.<AccountDeptRunningWater>lambdaQuery()
                    .eq(AccountDeptRunningWater::getRunningWaterId, accountRunningWater.getId()));
            if(ObjectUtil.isNotEmpty(accountDeptRunningWater)){
                //查询资金流水分拆部门表明细
                List<AccountDeptRunningWaterDetail> accountDeptRunningWaterDetailList = this.list(Wrappers.<AccountDeptRunningWaterDetail>lambdaQuery()
                        .eq(AccountDeptRunningWaterDetail::getDeptRunningWaterId, accountDeptRunningWater.getId()));

                //插入资金流水分拆部门表明细信息
                accountRunningWaterVo.setDeptRunningWaterDetailList(accountDeptRunningWaterDetailList);
                //插入资金流水-部门关联表ID
                accountRunningWaterVo.setAccountDeptRunningWaterId(accountDeptRunningWater.getId());
            }

        }

        return accountRunningWaterVo;
    }

    /**
     * 资金流水分拆部门表新增
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void add(AccountRunningWaterVo accountRunningWaterVo) {
        //判断是调整部门还是分拆（是否存在资金流水分拆部门表）
        List<AccountDeptRunningWaterDetail> deptRunningWaterDetailList = accountRunningWaterVo.getDeptRunningWaterDetailList();
        //修改资金流水-账户表的的分拆类型为已分拆
        AccountDeptRunningWater accountDeptRunningWater = new AccountDeptRunningWater();

        if (ObjectUtil.isNotEmpty(deptRunningWaterDetailList)){//分拆功能
            //分拆功能 添加或者修改资金流水分拆部门表的数据
          saveOrEditAccountDeptRunningWaterDetail(accountRunningWaterVo,accountDeptRunningWater,deptRunningWaterDetailList);
        }else {//调整部门功能
            //添加部门-资金流水关联表
            AccountDeptRunningWaterDetail accountDeptRunningWaterDetail = new AccountDeptRunningWaterDetail();
            accountDeptRunningWaterDetail.setDeptRunningWaterId(accountRunningWaterVo.getAccountDeptRunningWaterId());
            accountDeptRunningWaterDetail.setDeptId(accountRunningWaterVo.getDeptId());
            accountDeptRunningWaterDetail.setAmount(accountRunningWaterVo.getAmount());

            //添加资金流水分拆部门表
            accountDeptRunningWater.setRunningWaterId(accountRunningWaterVo.getId());
            accountDeptRunningWater.setType("10");
            //如果部门-资金流水关联表ID不为null修改资金流水分拆部门表的数据
           if (ObjectUtil.isNotEmpty(accountRunningWaterVo.getAccountDeptRunningWaterId())){
               //查询部门-资金流水关联表的数据（获取到部门-资金流水关联表中的数据是否分拆，如果是分拆则删除资金流水分拆部门表的数据，否则修改资金流水分拆部门表的数据在添加资金流水分拆部门表的数据）
               AccountDeptRunningWater accountDeptRunningWater1 = accountDeptRunningWaterService.getById(accountRunningWaterVo.getAccountDeptRunningWaterId());

               if (accountDeptRunningWater1.getType().equals("20")){
                   accountDeptRunningWater.setId(accountRunningWaterVo.getAccountDeptRunningWaterId());
                  accountDeptRunningWaterService.updateById(accountDeptRunningWater);
                   //删除资金流水分拆部门表的数据
                   baseMapper.delete(Wrappers.<AccountDeptRunningWaterDetail>lambdaQuery()
                           .eq(AccountDeptRunningWaterDetail::getDeptRunningWaterId,accountRunningWaterVo.getAccountDeptRunningWaterId()));
                   baseMapper.insert(accountDeptRunningWaterDetail);

               }else if (accountDeptRunningWater1.getType().equals("10")){
                   //修改资金流水分拆部门表的数据的数据
                   baseMapper.update(accountDeptRunningWaterDetail,Wrappers.<AccountDeptRunningWaterDetail>lambdaQuery()
                           .eq(AccountDeptRunningWaterDetail::getDeptRunningWaterId,accountRunningWaterVo.getAccountDeptRunningWaterId()));
               }
           }else {//如果部门-资金流水关联表ID为null添加资金流水分拆部门表的数据
               //添加部门-资金流水关联表的信息
               accountDeptRunningWaterService.save(accountDeptRunningWater);
               accountDeptRunningWaterDetail.setDeptRunningWaterId(accountDeptRunningWater.getId());
               baseMapper.insert(accountDeptRunningWaterDetail);
           }
        }

    }

    @Override
    public void edit(AccountDeptRunningWaterDetailDto accountDeptRunningWaterDetailDto) {
        this.updateById(accountDeptRunningWaterDetailDto);
    }

    @Override
    public void delete(Long id) {
        this.removeById(id);
    }

    //分拆功能 添加或者修改资金流水分拆部门表的数据
    private void saveOrEditAccountDeptRunningWaterDetail(AccountRunningWaterVo accountRunningWaterVo,
                                                         AccountDeptRunningWater accountDeptRunningWater,
                                                         List<AccountDeptRunningWaterDetail> deptRunningWaterDetailList
    ){
        //如果存在部门-资金流水关联表的ID则修改
        if (ObjectUtil.isNotEmpty(accountRunningWaterVo.getAccountDeptRunningWaterId())){
            accountDeptRunningWater.setId(accountRunningWaterVo.getAccountDeptRunningWaterId());
            accountDeptRunningWater.setType("20");
            accountDeptRunningWaterService.updateById(accountDeptRunningWater);

            //删除分拆部门表的数据
            //获取到有参数中有分拆部门详情表的ID
            List<Long> ids = deptRunningWaterDetailList.stream().map(deptRunningWaterDetail ->
                    deptRunningWaterDetail.getId()).collect(Collectors.toList());

            //删除数据库中不包含参数中有分拆部门详情表的ID的数据
            this.remove(Wrappers.<AccountDeptRunningWaterDetail>lambdaQuery()
                    .notIn(AccountDeptRunningWaterDetail::getId,ids).eq(AccountDeptRunningWaterDetail::getDeptRunningWaterId,
                            accountRunningWaterVo.getAccountDeptRunningWaterId()));

        }else {
            accountDeptRunningWater.setRunningWaterId(accountRunningWaterVo.getId());
            accountDeptRunningWater.setType("20");
            accountDeptRunningWaterService.save(accountDeptRunningWater);
        }
        //获取分拆部门详情中ID为null的数据
        deptRunningWaterDetailList = deptRunningWaterDetailList.stream()
                .filter(deptRunningWaterDetail->deptRunningWaterDetail.getId()==null)
                .collect(Collectors.toList());

        //添加资金流水分拆部门表中ID为null的数据
        deptRunningWaterDetailList.forEach(deptRunningWaterDetail->deptRunningWaterDetail
                .setDeptRunningWaterId(accountRunningWaterVo.getAccountDeptRunningWaterId()));

        this.saveBatch(deptRunningWaterDetailList);
    }

}
