package cn.iocoder.yudao.module.jx.payment.api;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.jx.core.controller.BaseController;
import cn.iocoder.yudao.module.jx.domain.DriveSchoolCoach;
import cn.iocoder.yudao.module.jx.mapper.DriveSchoolCoachMapper;
import cn.iocoder.yudao.module.jx.payment.entity.DriveSchoolPay;
import cn.iocoder.yudao.module.jx.payment.service.DriveSchoolPayService;
import cn.iocoder.yudao.module.jx.payment.vo.PayVo;
import cn.iocoder.yudao.module.jx.service.IDriveSchoolInfoService;
import cn.iocoder.yudao.module.jx.vo.DriveSchoolInfoVO;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.service.permission.PermissionService;
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 订单Controller
 *
 * @author ruoyi
 * @date 2024-04-24
 */
@RestController
@RequestMapping("/admin-api/system/pay")
public class DriveSchoolPayController extends BaseController
{
    @Autowired
    private DriveSchoolPayService driveSchoolPayService;
    @Autowired
    private IDriveSchoolInfoService driveSchoolInfoService;

    @Resource
    private AdminUserApi userApi;
    @Resource
    private PermissionService permissionService;
    @Resource
    private AdminUserService userService;

    @GetMapping("updatePayById")
    public void updatePayById(Integer id, String name, Integer jlId) {
        driveSchoolPayService.updatePayById(id, name, jlId);
    }


    /**
     * 查询订单列表
     */
    @GetMapping("/list")
    public CommonResult<IPage<?>> list(DriveSchoolPay driveSchoolPay) {

        // 获取当前用户信息
        LoginUser user = SecurityFrameworkUtils.getLoginUser();

        //获取驾校ID
        Long userId = user.getId();
        AdminUserDO sysUser = userService.getUser(userId);
        Long deptId = sysUser.getDeptId();

        // 记录部门ID
        System.out.println("User ID: " + userId + " | Dept ID: " + deptId);

        // 获取驾校信息
        //schoolInfo为null
        DriveSchoolInfoVO schoolInfo = driveSchoolInfoService.getSchoolInfoByDeptId(deptId);

        // 设置驾校ID
        if (schoolInfo != null) {
            System.out.println("啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊");
            driveSchoolPay.setJxId(schoolInfo.getId());
        }

        // 创建分页对象
        Page<DriveSchoolPay> page = new Page<>(driveSchoolPay.getPageNum(), driveSchoolPay.getPageSize());
        IPage<DriveSchoolPay> driveSchoolPayIPage = driveSchoolPayService.selectDriveSchoolPayList(driveSchoolPay, page);

        // 处理记录格式化排序
        List<DriveSchoolPay> list = driveSchoolPayIPage.getRecords();
        int allNumber = (driveSchoolPay.getPageNum() - 1) * driveSchoolPay.getPageSize();
        for (int i = 0; i < list.size(); i++) {
            String formattedNumber = String.format("%05d", allNumber + i + 1);
            list.get(i).setSorNumber(formattedNumber);
        }

        return CommonResult.success(driveSchoolPayIPage);
    }

    /**
     * 查询订单列表
     */
    @GetMapping("/stuList")
    public CommonResult<IPage<?>> stuList(DriveSchoolPay driveSchoolPay)
    {
        Page<DriveSchoolPay> page = new Page<>(driveSchoolPay.getPageNum(), driveSchoolPay.getPageSize());
        IPage<DriveSchoolPay> driveSchoolPayIPage = driveSchoolPayService.stuList(driveSchoolPay, page);
        return CommonResult.success(driveSchoolPayIPage);
    }


//    @Resource
//    private SysUserMapper sysUserMapper;

    @Resource
    private DriveSchoolCoachMapper driveSchoolCoachMapper;

    /**
     * 查询订单列表
     */
    @GetMapping("/stuListByCoachId")
    public CommonResult<IPage<?>> stuListByCoachId(DriveSchoolPay driveSchoolPay)
    {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        AdminUserRespDTO sysUser = userApi.getUser(userId);
        String phonenumber = sysUser.getMobile();
//        Long userId = SecurityUtils.getUserId();
//        SysUser sysUser = sysUserMapper.selectUserById(userId);
//        String phonenumber = sysUser.getPhonenumber();
        DriveSchoolCoach driveSchoolCoach = driveSchoolCoachMapper.selectByPhonenumber(phonenumber);
        Long jlId = 0L;
        if (ObjectUtils.isNotEmpty(driveSchoolCoach)){
            jlId= driveSchoolCoach.getId();
        }
        driveSchoolPay.setJlId(jlId);
        Page<DriveSchoolPay> page = new Page<>(driveSchoolPay.getPageNum(), driveSchoolPay.getPageSize());
        IPage<DriveSchoolPay> driveSchoolPayIPage = driveSchoolPayService.stuListByCoachId(driveSchoolPay, page);
        return CommonResult.success(driveSchoolPayIPage);
    }


    /**
     * 导出订单列表
     */
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, DriveSchoolPay driveSchoolPay)
//    {
//        List<DriveSchoolPay> list = driveSchoolPayService.selectDriveSchoolPayList(driveSchoolPay);
//        ExcelUtil<DriveSchoolPay> util = new ExcelUtil<DriveSchoolPay>(DriveSchoolPay.class);
//        util.exportExcel(response, list, "订单数据");
//    }

    /**
     * 导入学员信息
     */
//    @PostMapping("/exportIn")
//    public void exportIn(HttpServletResponse response, DriveSchoolPay driveSchoolPay)
//    {
//        List<DriveSchoolPay> list = driveSchoolPayService.selectDriveSchoolPayList(driveSchoolPay);
//        ExcelUtil<DriveSchoolPay> util = new ExcelUtil<DriveSchoolPay>(DriveSchoolPay.class);
//        util.exportExcel(response, list, "订单数据");
//    }



//    @PostMapping("/importData")
//    public CommonResult importData(MultipartFile file, boolean updateSupport) throws Exception
//    {
//        ExcelUtil<DriveSchoolPay> util = new ExcelUtil<DriveSchoolPay>(DriveSchoolPay.class);
//        List<DriveSchoolPay> userList = util.importExcel(file.getInputStream());
//        String operName = getUsername();
//        String message = driveSchoolPayService.importUser(userList, updateSupport, operName);
//        return success(message);
//    }


    /**
     * 获取订单详细信息
     */
    @GetMapping(value = "/{id}")
    public CommonResult getInfo(@PathVariable("id") Long id)
    {
        return success(driveSchoolPayService.selectDriveSchoolPayById(id));
    }

    /**
     * 获取订单详细信息
     */
    @GetMapping(value = "/getInfoByUserId/{userId}")
    public CommonResult getInfoByUserId(@PathVariable("userId") Long userId)
    {
        return success(driveSchoolPayService.getInfoByUserId(userId));
    }

    /**
     * 获取订单详细信息
     */
    @GetMapping(value = "/getInfoByUserIndenty/{userId}")
    public CommonResult getInfoByUserIndenty(@PathVariable("userId") String userId)
    {
        return success(driveSchoolPayService.getInfoByUserIndenty(userId));
    }

    /**
     * 新增订单
     */
    @PostMapping
    public CommonResult add(@RequestBody DriveSchoolPay driveSchoolPay)
    {
        return toAjax(driveSchoolPayService.insertDriveSchoolPay(driveSchoolPay));
    }

    /**
     * 修改订单
     */
    @PutMapping
    public CommonResult edit(@RequestBody DriveSchoolPay driveSchoolPay)
    {
        return toAjax(driveSchoolPayService.updateDriveSchoolPay(driveSchoolPay));
    }

    /**
     * 删除订单
     */
    @DeleteMapping("/{ids}")
    public CommonResult remove(@PathVariable Long[] ids)
    {
        return toAjax(driveSchoolPayService.deleteDriveSchoolPayByIds(ids));
    }

    /**
     * 学员详情-报名课程接口
     */
    @GetMapping("/applet/getReportCourseList")
    public CommonResult<IPage<?>> getReportCourseList(DriveSchoolPay driveSchoolPay)
    {
        Page<DriveSchoolPay> page = new Page<>(driveSchoolPay.getPageNum(), driveSchoolPay.getPageSize());
        IPage<DriveSchoolPay> reportCourseList = driveSchoolPayService.getReportCourseList(driveSchoolPay, page);
        return CommonResult.success(reportCourseList);
    }

    /**
     * 学员详情列表
     */
    @GetMapping("/applet/getUserList")
    public CommonResult<IPage<?>> getUserList(DriveSchoolPay driveSchoolPay)
    {
        Page<DriveSchoolPay> page = new Page<>(driveSchoolPay.getPageNum(), driveSchoolPay.getPageSize());
        IPage<DriveSchoolPay> userList = driveSchoolPayService.getUserList(driveSchoolPay, page);
        return CommonResult.success(userList);
    }

    /**
     * 返回教练提成金额
     */
    @GetMapping("/getDeductByUserInfo")
    public CommonResult getDeductByUserInfo(DriveSchoolPay driveSchoolPay)
    {
        PayVo payVo = driveSchoolPayService.getDeductByUserInfo(driveSchoolPay);

        return CommonResult.success(payVo);
    }

}
