package com.srs.workstudy.service.impl;

import java.util.List;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.srs.common.core.domain.AjaxResult;
import com.srs.common.core.domain.entity.SysDept;
import com.srs.common.core.domain.entity.SysUser;
import com.srs.comprehensive.domain.SrsStudent;
import com.srs.comprehensive.mapper.SrsStudentMapper;
import com.srs.system.mapper.SysDeptMapper;
import com.srs.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.srs.workstudy.mapper.QgzxMoneyExtraMapper;
import com.srs.workstudy.domain.QgzxMoneyExtra;
import com.srs.workstudy.service.IQgzxMoneyExtraService;

import static com.srs.common.utils.DateUtils.getNowDate;
import static com.srs.common.utils.SecurityUtils.getUserId;
import static com.srs.common.utils.SecurityUtils.getUsername;

/**
 * 勤工助学额外薪资Service业务层处理
 *
 * @author srs
 * @date 2024-11-10
 */
@Service
public class QgzxMoneyExtraServiceImpl extends ServiceImpl<QgzxMoneyExtraMapper,QgzxMoneyExtra> implements IQgzxMoneyExtraService {
    @Autowired
    private QgzxMoneyExtraMapper qgzxMoneyExtraMapper;

    @Autowired
    private SysUserMapper _userMapper;

    @Autowired
    private SysDeptMapper _deptMapper;

    @Autowired
    private SrsStudentMapper _srsStudentMapper;

    @Override
    public AjaxResult delMoneyExtra(Long id){
        try{
            int res = qgzxMoneyExtraMapper.deleteById(id);
            if (res == 0) {
                throw new Exception("删除失败");
            }

            return AjaxResult.success();
        }catch(Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    @Override
    public AjaxResult addMoneyExtra(QgzxMoneyExtra param){
        try{
            SysDept dept = _deptMapper.selectById(param.getDeptId());
            if (dept == null) {
                throw new Exception("找不到部门");
            }
            param.deptCode = dept.getDeptCode();

            param.auditRemark = "-资助办领导手动添加-";
            SysUser user = _userMapper.selectUserById(getUserId());
            if (user.getSignature() == null || Objects.equals(user.getSignature(), "")) {
                throw new Exception("您的签名未上传");
            }

            String finalSign = user.getSignature();
            param.finalSign = finalSign;

            SysUser zdls = _userMapper.selectUserByUserName(param.zdlsNo);
            if (zdls == null) {
                throw new Exception("找不到指导老师");
            }

            if (zdls.getSignature() == null || Objects.equals(zdls.getSignature(), "")) {
                throw new Exception("指导老师签名未上传");
            }
            param.zdlsSign = zdls.getSignature();

            QueryWrapper<SrsStudent> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("stu_no", param.stuNo);
            SrsStudent student = _srsStudentMapper.selectOne(queryWrapper);
            if (student == null) {
                throw new Exception("找不到学生");
            }
            param.bankCard = student.xhk;
            param.bankAddr = student.bankAddr;
            param.applyStatus = "6";
            param.finalNo = getUsername();
            param.finalTime = getNowDate();

            int res = qgzxMoneyExtraMapper.insert(param);
            if (res == 0) {
                throw new Exception("添加失败");
            }



            return AjaxResult.success();
        }catch(Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 查询勤工助学额外薪资
     *
     * @param id 勤工助学额外薪资主键
     * @return 勤工助学额外薪资
     */
    @Override
    public QgzxMoneyExtra selectQgzxMoneyExtraById(Long id) {
        return qgzxMoneyExtraMapper.selectQgzxMoneyExtraById(id);
    }

    /**
     * 查询勤工助学额外薪资列表
     *
     * @param qgzxMoneyExtra 勤工助学额外薪资
     * @return 勤工助学额外薪资
     */
    @Override
    public List<QgzxMoneyExtra> selectQgzxMoneyExtraList(QgzxMoneyExtra qgzxMoneyExtra) {
        return qgzxMoneyExtraMapper.selectQgzxMoneyExtraList(qgzxMoneyExtra);
    }

    /**
     * 新增勤工助学额外薪资
     *
     * @param qgzxMoneyExtra 勤工助学额外薪资
     * @return 结果
     */
    @Override
    public int insertQgzxMoneyExtra(QgzxMoneyExtra qgzxMoneyExtra) {
            return qgzxMoneyExtraMapper.insertQgzxMoneyExtra(qgzxMoneyExtra);
    }

    /**
     * 修改勤工助学额外薪资
     *
     * @param qgzxMoneyExtra 勤工助学额外薪资
     * @return 结果
     */
    @Override
    public int updateQgzxMoneyExtra(QgzxMoneyExtra qgzxMoneyExtra) {
        return qgzxMoneyExtraMapper.updateQgzxMoneyExtra(qgzxMoneyExtra);
    }

    /**
     * 批量删除勤工助学额外薪资
     *
     * @param ids 需要删除的勤工助学额外薪资主键
     * @return 结果
     */
    @Override
    public int deleteQgzxMoneyExtraByIds(Long[] ids) {
        return qgzxMoneyExtraMapper.deleteQgzxMoneyExtraByIds(ids);
    }

    /**
     * 删除勤工助学额外薪资信息
     *
     * @param id 勤工助学额外薪资主键
     * @return 结果
     */
    @Override
    public int deleteQgzxMoneyExtraById(Long id) {
        return qgzxMoneyExtraMapper.deleteQgzxMoneyExtraById(id);
    }
}
