package cn.iocoder.yudao.module.jx.payment.service.impl;

import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.jx.domain.DriveSchoolCourse;
import cn.iocoder.yudao.module.jx.domain.DriveSchoolDeduct;
import cn.iocoder.yudao.module.jx.mapper.DriveSchoolCoachMapper;
import cn.iocoder.yudao.module.jx.mapper.DriveSchoolCourseMapper;
import cn.iocoder.yudao.module.jx.mapper.DriveSchoolDeductMapper;
import cn.iocoder.yudao.module.jx.payment.entity.DriveSchoolPay;
import cn.iocoder.yudao.module.jx.payment.mapper.DriveSchoolPayMapper;
import cn.iocoder.yudao.module.jx.payment.service.DriveSchoolPayService;
import cn.iocoder.yudao.module.jx.payment.vo.PayVo;
import cn.iocoder.yudao.module.jx.vo.DriveSchoolPayVo;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

/**
 * 订单Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-24
 */
@Service
public class DriveSchoolPayServiceImpl implements DriveSchoolPayService
{
    @Autowired
    private DriveSchoolPayMapper driveSchoolPayMapper;
//    @Resource
//    private SysUserMapper sysUserMapper;


    @Resource
    private AdminUserApi userApi;

    @Autowired
    private DriveSchoolCourseMapper driveSchoolCourseMapper;

    @Resource
    private DriveSchoolDeductMapper driveSchoolDeductMapper;

    /**
     * 查询订单
     *
     * @param id 订单主键
     * @return 订单
     */
    @Override
    public DriveSchoolPay selectDriveSchoolPayById(Long id)
    {
        DriveSchoolPay driveSchoolPay = driveSchoolPayMapper.selectDriveSchoolPayById(id);
        DriveSchoolCourse driveSchoolCourse = driveSchoolCourseMapper.selectDriveSchoolCourseById(driveSchoolPay.getCourseId());
        driveSchoolPay.setDriveSchoolCourse(driveSchoolCourse);
        return driveSchoolPay;
    }

    /**
     * 查询订单列表
     *
     * @param driveSchoolPay 订单
     * @return 订单
     */
    @Override
    public IPage<DriveSchoolPay> selectDriveSchoolPayList(DriveSchoolPay driveSchoolPay, Page<DriveSchoolPay> page)
    {
        IPage<DriveSchoolPay> driveSchoolPayIPage = driveSchoolPayMapper.selectDriveSchoolPayList(driveSchoolPay, page);
        List<DriveSchoolPay> driveSchoolPays = driveSchoolPayIPage.getRecords();
        driveSchoolPays.forEach(item->{
            DriveSchoolCourse driveSchoolCourse = driveSchoolCourseMapper.selectDriveSchoolCourseById(item.getCourseId());
            if (ObjectUtils.isEmpty(driveSchoolCourse)){
                DriveSchoolCourse driveSchoolCourse1 = new DriveSchoolCourse();
                item.setDriveSchoolCourse(driveSchoolCourse1);
            }else {
                item.setDriveSchoolCourse(driveSchoolCourse);
            }
            if (ObjectUtils.isNotEmpty(driveSchoolCourse)){
                item.setCourseName(driveSchoolCourse.getAutomatic());
            }
        });
        return driveSchoolPayIPage;
    }

    @Override
    public IPage<DriveSchoolPay> stuList(DriveSchoolPay driveSchoolPay,Page<DriveSchoolPay> page)
    {
        IPage<DriveSchoolPay> driveSchoolPayIPage = driveSchoolPayMapper.stuList(driveSchoolPay, page);
        List<DriveSchoolPay> driveSchoolPays = driveSchoolPayIPage.getRecords();

        ArrayList<DriveSchoolPay> collect = driveSchoolPays.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(DriveSchoolPay::getIdentity))), ArrayList::new));

        for (DriveSchoolPay schoolPay : collect) {
            Long userId = schoolPay.getUserId();
            AdminUserRespDTO sysUser = userApi.getUser(userId);
            //SysUser sysUser = sysUserMapper.selectUserById(userId);
            if (ObjectUtils.isNotEmpty(sysUser)){
                schoolPay.setVactor(sysUser.getAvatar());
            }
        }
        //根据时间倒序排序
        collect.sort(Comparator.comparing(DriveSchoolPay::getCreateTime).reversed());
        driveSchoolPayIPage.setRecords(collect);
        return driveSchoolPayIPage;
    }

    /**
     * 新增订单
     *
     * @param driveSchoolPay 订单
     * @return 结果
     */
    @Override
    public int insertDriveSchoolPay(DriveSchoolPay driveSchoolPay)
    {
        return driveSchoolPayMapper.insertDriveSchoolPay(driveSchoolPay);
    }

    /**
     * 修改订单
     *
     * @param driveSchoolPay 订单
     * @return 结果
     */
    @Override
    public int updateDriveSchoolPay(DriveSchoolPay driveSchoolPay)
    {
        return driveSchoolPayMapper.updateDriveSchoolPay(driveSchoolPay);
    }

    /**
     * 批量删除订单
     *
     * @param ids 需要删除的订单主键
     * @return 结果
     */
    @Override
    public int deleteDriveSchoolPayByIds(Long[] ids)
    {
        return driveSchoolPayMapper.deleteDriveSchoolPayByIds(ids);
    }

    /**
     * 删除订单信息
     *
     * @param id 订单主键
     * @return 结果
     */
    @Override
    public int deleteDriveSchoolPayById(Long id)
    {
        return driveSchoolPayMapper.deleteDriveSchoolPayById(id);
    }

    @Override
    public void updatePayById(Integer id, String name, Integer jlId) {
        driveSchoolPayMapper.updatePayById(id, name, jlId);
    }

    @Override
    public IPage<DriveSchoolPay> getReportCourseList(DriveSchoolPay driveSchoolPay,Page<DriveSchoolPay> page) {
        /*Long deptId = SecurityUtils.getDeptId();
        driveSchoolPay.setJxId(deptId);*/
        IPage<DriveSchoolPay> reportCourseList = driveSchoolPayMapper.getReportCourseList(driveSchoolPay, page);
        return reportCourseList;
    }

    @Override
    public IPage<DriveSchoolPay> getUserList(DriveSchoolPay driveSchoolPay,Page<DriveSchoolPay> page) {
        return driveSchoolPayMapper.getUserList(driveSchoolPay,page);

//        ArrayList<DriveSchoolPayVo> collect = userList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() ->
//                new TreeSet<>(Comparator.comparing(DriveSchoolPay::getPhone))), ArrayList::new));
        /*for (DriveSchoolPayVo driveSchoolPayVo : userList) {
            driveSchoolPayMapper.getInfoByUserIndenty(driveSchoolPayVo.getIdentity())
            SysUser sysUserData = sysUserMapper.findSysUserById(driveSchoolPayVo.getUserId());
            driveSchoolPayVo.setPhone(sysUserData.getPhonenumber());
        }*/
       // return userList;
    }

    @Override
    public DriveSchoolPay getInfoByUserId(Long userId) {
        List<DriveSchoolPay> infoByUserId = driveSchoolPayMapper.getInfoByUserId(userId);
        DriveSchoolPay driveSchoolPay = infoByUserId.get(0);
        DriveSchoolCourse driveSchoolCourse = driveSchoolCourseMapper.selectDriveSchoolCourseById(driveSchoolPay.getCourseId());
        driveSchoolPay.setDriveSchoolCourse(driveSchoolCourse);
        return driveSchoolPay;
    }

    @Override
    public DriveSchoolPay getInfoByUserIndenty(String userId) {
        List<DriveSchoolPay> infoByUserId = driveSchoolPayMapper.getInfoByUserIndenty(userId);
        DriveSchoolPay driveSchoolPay = infoByUserId.get(0);
        DriveSchoolCourse driveSchoolCourse = driveSchoolCourseMapper.selectDriveSchoolCourseById(driveSchoolPay.getCourseId());
        driveSchoolPay.setDriveSchoolCourse(driveSchoolCourse);
        return driveSchoolPay;
    }

    @Override
    public PayVo getDeductByUserInfo(DriveSchoolPay driveSchoolPay) {
        PayVo payVo = new PayVo();
        //获取订单信息
        DriveSchoolPay deductByUserInfo = driveSchoolPayMapper.getDeductByUserInfo(driveSchoolPay);
        if (ObjectUtils.isNotEmpty(deductByUserInfo)){
            //计算提成金额
            DriveSchoolCourse driveSchoolCourse = driveSchoolCourseMapper.selectDriveSchoolCourseById(deductByUserInfo.getCourseId());
            BigDecimal price = driveSchoolCourse.getPrice();
            double doubleValue = price.doubleValue();

            Long deptId = SecurityFrameworkUtils.getLoginUserDeptId();
            //Long deptId = SecurityUtils.getDeptId();
            String courseSubject = driveSchoolPay.getCourseSubject();
            DriveSchoolDeduct driveSchoolDeduct = driveSchoolDeductMapper.selectByCondition(deptId, courseSubject);
            BigDecimal deduct = driveSchoolDeduct.getDeduct();

            payVo.setDeduct(deduct.doubleValue());
        }
        return payVo;
    }

    @Resource
    private DriveSchoolCoachMapper driveSchoolCoachMapper;

    @Override
    public IPage<DriveSchoolPay> stuListByCoachId(DriveSchoolPay driveSchoolPay,Page<DriveSchoolPay> page) {
        //获取教练id
        List<DriveSchoolPay> driveSchoolPays = new ArrayList<>();
        IPage<DriveSchoolPay> driveSchoolPayIPage = new Page<>();
        if (ObjectUtils.isNotEmpty(driveSchoolPay.getType())){
            if (driveSchoolPay.getType().equals("2")){
                driveSchoolPay.setType("科目一");
                driveSchoolPayIPage = driveSchoolPayMapper.stuOneList(driveSchoolPay, page);
                driveSchoolPays = driveSchoolPayIPage.getRecords();

            }else {
                driveSchoolPay.setType("科目二");
                driveSchoolPayIPage = driveSchoolPayMapper.stuTwoList(driveSchoolPay, page);
                driveSchoolPays = driveSchoolPayIPage.getRecords();
            }
        }

        Long userIds = SecurityFrameworkUtils.getLoginUserId();
        driveSchoolPay.setJlId(driveSchoolPay.getJlId());

        for (DriveSchoolPay schoolPay : driveSchoolPays) {
            AdminUserRespDTO sysUser = userApi.getUser(userIds);
            //SysUser sysUser = sysUserMapper.selectUserById(userIds);
            schoolPay.setVactor(sysUser.getAvatar());
            DriveSchoolCourse driveSchoolCourse = driveSchoolCourseMapper.selectDriveSchoolCourseById(schoolPay.getCourseId());
            if(ObjectUtils.isNotEmpty(driveSchoolCourse)){
                schoolPay.setCourseName(driveSchoolCourse.getName());
            }
        }

        return driveSchoolPayIPage;
    }

    @Override
    public DriveSchoolPay selectByUserId(Long userId) {
        return driveSchoolPayMapper.selectByUserId(userId);
    }


    @Override
    public String importUser(List<DriveSchoolPay> userList, boolean updateSupport, String operName) throws Exception {
        if (CollectionUtils.isEmpty(userList)){
            throw new Exception("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        for (DriveSchoolPay driveSchoolPay : userList) {
            DriveSchoolPay driveSchoolPay1 = driveSchoolPayMapper.selectByPhone(driveSchoolPay.getPhone());
            if (ObjectUtils.isEmpty(driveSchoolPay1)){
                driveSchoolPayMapper.insertDriveSchoolPay(driveSchoolPay);
                successNum++;
            }else {
                failureNum++;
            }
        }
        return "数据已全部导入成功！";
    }

    @Override
    public double getAllMoney() {
        return driveSchoolPayMapper.getAllMoney();
    }
}
