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

import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.jx.domain.DriveContractTest;
import cn.iocoder.yudao.module.jx.domain.DriveSchoolContract;
import cn.iocoder.yudao.module.jx.domain.DriveSchoolCourse;
import cn.iocoder.yudao.module.jx.mapper.DriveContractTestMapper;
import cn.iocoder.yudao.module.jx.mapper.DriveSchoolContractMapper;
import cn.iocoder.yudao.module.jx.mapper.DriveSchoolCourseMapper;
import cn.iocoder.yudao.module.jx.payment.entity.DriveSchoolPay;
import cn.iocoder.yudao.module.jx.payment.mapper.DriveSchoolPayMapper;
import cn.iocoder.yudao.module.jx.service.IDriveSchoolContractService;
import cn.iocoder.yudao.module.jx.utils.DateUtils;
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.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * 驾校合同Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-25
 */
@Service
public class DriveSchoolContractServiceImpl implements IDriveSchoolContractService
{
    @Autowired
    private DriveSchoolContractMapper driveSchoolContractMapper;


    @Autowired
    private DriveContractTestMapper driveContractTestMapper;

    @Autowired
    private DriveSchoolPayMapper driveSchoolPayMapper;

    @Autowired
    private DriveSchoolCourseMapper driveSchoolCourseMapper;

    @Resource
    private AdminUserApi userApi;

    /**
     * 查询驾校合同
     *
     * @param id 驾校合同主键
     * @return 驾校合同
     */
    @Override
    public DriveSchoolContract selectDriveSchoolContractById(Long id)
    {
        return driveSchoolContractMapper.selectDriveSchoolContractById(id);
    }

    /**
     * 查询驾校合同列表
     *
     * @param driveSchoolContract 驾校合同
     * @return 驾校合同
     */
    @Override
    public IPage<DriveSchoolContract> selectDriveSchoolContractList(DriveSchoolContract driveSchoolContract, Page<DriveSchoolContract> page)
    {
        return driveSchoolContractMapper.selectDriveSchoolContractList(driveSchoolContract,page);
    }

    /**
     * 新增驾校合同
     *
     * @param driveSchoolContract 驾校合同
     * @return 结果
     */
    @Override
    public int insertDriveSchoolContract(DriveSchoolContract driveSchoolContract)
    {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        AdminUserRespDTO user = userApi.getUser(userId);
        Long deptId = user.getDeptId();
        driveSchoolContract.setDeptId(deptId);
        driveSchoolContract.setUserId(userId);

        //获取合同模板
        DriveContractTest driveContractTest = driveContractTestMapper.selectDriveContractTestById(1L);
        String content = driveContractTest.getContent();

        String name = content.replaceAll("name", driveSchoolContract.getUserName());
        String adress = name.replaceAll("adress", driveSchoolContract.getAddress());

        //driveSchoolContract.setCreateTime(DateUtils.getNowDate());

        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentDate = sdf.format(date);
        String time = adress.replaceAll("time", currentDate);
        // content.replaceAll("timend",currentDate);
        DriveSchoolPay driveSchoolPay = driveSchoolPayMapper.selectByUserId(userId);
        String indent = time.replaceAll("indent", driveSchoolPay.getIdentity());
        String phone = indent.replaceAll("phone", driveSchoolPay.getPhone());
        //课程
        DriveSchoolCourse driveSchoolCourse = driveSchoolCourseMapper.selectDriveSchoolCourseById(driveSchoolPay.getCourseId());
        String type = "";
        if (driveSchoolCourse.getName().equals("1")){
            type = phone.replaceAll("type", "C1");
        }else {
            type = phone.replaceAll("type", "C2");
        }

        String carType = type.replaceAll("carType", driveSchoolCourse.getName());
        String money = carType.replaceAll("money", driveSchoolCourse.getReserveMoney().toString());

        //driveSchoolContract.setCreateTime(DateUtils.getNowDate());
        driveSchoolContract.setContent(money);
        return driveSchoolContractMapper.insertDriveSchoolContract(driveSchoolContract);
    }

    /**
     * 修改驾校合同
     *
     * @param driveSchoolContract 驾校合同
     * @return 结果
     */
    @Override
    public int updateDriveSchoolContract(DriveSchoolContract driveSchoolContract)
    {
       // driveSchoolContract.setUpdateTime(DateUtils.getNowDate());
        return driveSchoolContractMapper.updateDriveSchoolContract(driveSchoolContract);
    }

    /**
     * 批量删除驾校合同
     *
     * @param ids 需要删除的驾校合同主键
     * @return 结果
     */
    @Override
    public int deleteDriveSchoolContractByIds(Long[] ids)
    {
        return driveSchoolContractMapper.deleteDriveSchoolContractByIds(ids);
    }

    /**
     * 删除驾校合同信息
     *
     * @param id 驾校合同主键
     * @return 结果
     */
    @Override
    public int deleteDriveSchoolContractById(Long id)
    {
        return driveSchoolContractMapper.deleteDriveSchoolContractById(id);
    }
}
