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

import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.jx.domain.DriveSchoolFeedback;
import cn.iocoder.yudao.module.jx.domain.DriveSchoolInfo;
import cn.iocoder.yudao.module.jx.mapper.DriveSchoolFeedbackMapper;
import cn.iocoder.yudao.module.jx.mapper.DriveSchoolInfoMapper;
import cn.iocoder.yudao.module.jx.payment.entity.DriveSchoolPay;
import cn.iocoder.yudao.module.jx.payment.service.DriveSchoolPayService;
import cn.iocoder.yudao.module.jx.service.IDriveSchoolFeedbackService;
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 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.stereotype.Service;

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

/**
 * 反馈Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-27
 */
@Service
public class DriveSchoolFeedbackServiceImpl implements IDriveSchoolFeedbackService
{
    @Resource
    private DriveSchoolFeedbackMapper driveSchoolFeedbackMapper;

//    @Resource
//    private SysUserMapper sysUserMapper;

    @Resource
    private AdminUserApi userApi;
    /**
     * 查询反馈
     *
     * @param id 反馈主键
     * @return 反馈
     */
    @Override
    public DriveSchoolFeedback selectDriveSchoolFeedbackById(Long id)
    {
        return driveSchoolFeedbackMapper.selectDriveSchoolFeedbackById(id);
    }

    @Resource
    private DriveSchoolInfoMapper driveSchoolInfoMapper;
    /**
     * 查询反馈列表
     *
     * @param driveSchoolFeedback 反馈
     * @return 反馈
     */
    @Override
    public IPage<DriveSchoolFeedback> selectDriveSchoolFeedbackList(DriveSchoolFeedback driveSchoolFeedback, Page<DriveSchoolFeedback> page)
    {
        Long deptId = SecurityFrameworkUtils.getLoginUserDeptId();
        //Long deptId = SecurityUtils.getDeptId();
        if(deptId !=100L){
            driveSchoolFeedback.setJxId(deptId);
        }
        return driveSchoolFeedbackMapper.selectDriveSchoolFeedbackList(driveSchoolFeedback,page);
    }

    @Override
    public List<DriveSchoolFeedback> selectDriveSchoolFeedbackListAll(DriveSchoolFeedback driveSchoolFeedback)
    {
        Long deptId = SecurityFrameworkUtils.getLoginUserDeptId();
        //Long deptId = SecurityUtils.getDeptId();
        if(deptId !=100L){
            driveSchoolFeedback.setJxId(deptId);
        }
        return driveSchoolFeedbackMapper.selectDriveSchoolFeedbackListAll(driveSchoolFeedback);
    }

    @Override
    public Map<Object,Object> like(DriveSchoolFeedback driveSchoolFeedback)
    {
        HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
        Long deptId = SecurityFrameworkUtils.getLoginUserDeptId();
        //Long deptId = SecurityUtils.getDeptId();
        if(deptId !=100L){
            driveSchoolFeedback.setJxId(deptId);
        }
        objectObjectHashMap.put("likeOne",driveSchoolFeedbackMapper.likeOne(driveSchoolFeedback));
        objectObjectHashMap.put("likeTwo",driveSchoolFeedbackMapper.likeTwo(driveSchoolFeedback));
        objectObjectHashMap.put("likeThree",driveSchoolFeedbackMapper.likeThree(driveSchoolFeedback));
        return objectObjectHashMap;
    }

    @Autowired
    private DriveSchoolPayService driveSchoolPayService;

    /**
     * 新增反馈
     *
     * @param driveSchoolFeedback 反馈
     * @return 结果
     */
    @Override
    public int insertDriveSchoolFeedback(DriveSchoolFeedback driveSchoolFeedback) throws Exception {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        AdminUserRespDTO sysUser = userApi.getUser(userId);
        if(ObjectUtils.isNotEmpty(sysUser)){
            driveSchoolFeedback.setUserName(sysUser.getNickname());
        }
        DriveSchoolPay driveSchoolPay = driveSchoolPayService.selectByUserId(userId);
        if (ObjectUtils.isEmpty(driveSchoolPay)){
            throw new RuntimeException("请先报名！");
        }
        driveSchoolFeedback.setUserId(userId);
        driveSchoolFeedback.setJlId(driveSchoolPay.getJlId());
        driveSchoolFeedback.setJlName(driveSchoolPay.getJlName());
        DriveSchoolInfoVO schoolInfoByDeptId = driveSchoolInfoMapper.findInfoByJxId(driveSchoolPay.getJxId());
        driveSchoolFeedback.setJxId(schoolInfoByDeptId.getDeptId());

        DriveSchoolInfo driveSchoolInfo = driveSchoolInfoMapper.selectDriveSchoolInfoById(driveSchoolPay.getJxId());
        driveSchoolFeedback.setJxName(driveSchoolInfo.getSchoolName());

        return driveSchoolFeedbackMapper.insertDriveSchoolFeedback(driveSchoolFeedback);
    }

    /**
     * 修改反馈
     *
     * @param driveSchoolFeedback 反馈
     * @return 结果
     */
    @Override
    public int updateDriveSchoolFeedback(DriveSchoolFeedback driveSchoolFeedback)
    {
        //driveSchoolFeedback.setUpdateTime(DateUtils.getNowDate());
        return driveSchoolFeedbackMapper.updateDriveSchoolFeedback(driveSchoolFeedback);
    }

    /**
     * 批量删除反馈
     *
     * @param ids 需要删除的反馈主键
     * @return 结果
     */
    @Override
    public int deleteDriveSchoolFeedbackByIds(Long[] ids)
    {
        return driveSchoolFeedbackMapper.deleteDriveSchoolFeedbackByIds(ids);
    }

    /**
     * 删除反馈信息
     *
     * @param id 反馈主键
     * @return 结果
     */
    @Override
    public int deleteDriveSchoolFeedbackById(Long id)
    {
        return driveSchoolFeedbackMapper.deleteDriveSchoolFeedbackById(id);
    }

    @Override
    public void insertData(String content) {
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        driveSchoolFeedbackMapper.insertData(loginUserId, content);

    }
}
