package com.srs.workstudy.service.impl;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.TemporalAdjusters;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.srs.common.core.domain.AjaxResult;
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.QgzxMonthAssessMapper;
import com.srs.workstudy.domain.QgzxMonthAssess;
import com.srs.workstudy.service.IQgzxMonthAssessService;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import static com.srs.common.utils.DateUtils.getNowDate;

/**
 * 勤工助学月度考核Service业务层处理
 *
 * @author srs
 * @date 2024-06-17
 */
@Service
public class QgzxMonthAssessServiceImpl extends ServiceImpl<QgzxMonthAssessMapper, QgzxMonthAssess> implements IQgzxMonthAssessService {
    @Autowired
    private QgzxMonthAssessMapper qgzxMonthAssessMapper;

    @Autowired
    public PlatformTransactionManager transactionManager;


    @Override
    public AjaxResult manyAssess(List<QgzxMonthAssess> param) {
        TransactionDefinition def = new DefaultTransactionDefinition();
        TransactionStatus status = transactionManager.getTransaction(def);
        try {

            List<Long> stuPostIds = param.stream().map(x -> x.stuPostId).collect(Collectors.toList());

            LocalDate now = getNowDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

            LocalDate firstDayOfMonth = now.with(TemporalAdjusters.firstDayOfMonth());
            LocalDate firstDayOfNextMonth = now.with(TemporalAdjusters.firstDayOfNextMonth());

            QueryWrapper<QgzxMonthAssess> deleteQuery = new QueryWrapper<>();
            deleteQuery.in("stu_post_id", stuPostIds)
                    .ge("submit_time", firstDayOfMonth)
                    .lt("submit_time", firstDayOfNextMonth);
            qgzxMonthAssessMapper.delete(deleteQuery);

            param.forEach(x -> x.submitTime = getNowDate());

            boolean res = this.saveBatch(param);
            if (!res) {
                throw new Exception("批量添加考核记录失败");
            }

            transactionManager.commit(status);
            return AjaxResult.success();
        } catch (Exception ex) {
            transactionManager.rollback(status);
            return AjaxResult.error(ex.getMessage());
        }
    }


    @Override
    public AjaxResult doAssess(QgzxMonthAssess param) {
        TransactionDefinition def = new DefaultTransactionDefinition();
        TransactionStatus status = transactionManager.getTransaction(def);
        try {

            LocalDate now = getNowDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

            LocalDate firstDayOfMonth = now.with(TemporalAdjusters.firstDayOfMonth());
            LocalDate firstDayOfNextMonth = now.with(TemporalAdjusters.firstDayOfNextMonth());

            QueryWrapper<QgzxMonthAssess> deleteQuery = new QueryWrapper<>();
            deleteQuery.eq("stu_post_id", param.getStuPostId())
                    .ge("submit_time", firstDayOfMonth)
                    .lt("submit_time", firstDayOfNextMonth);

            qgzxMonthAssessMapper.delete(deleteQuery);

            param.submitTime = getNowDate();

            int res = qgzxMonthAssessMapper.insert(param);
            if (res == 0) {
                throw new Exception("添加月度考核记录失败");
            }

            transactionManager.commit(status);
            return AjaxResult.success();
        } catch (Exception ex) {
            transactionManager.rollback(status);
            return AjaxResult.error(ex.getMessage());
        }
    }


    @Override
    public List<QgzxMonthAssess> listView(QgzxMonthAssess param) {
        return qgzxMonthAssessMapper.listView(param);
    }


    /**
     * 查询勤工助学月度考核
     *
     * @param id 勤工助学月度考核主键
     * @return 勤工助学月度考核
     */
    @Override
    public QgzxMonthAssess selectQgzxMonthAssessById(Long id) {
        return qgzxMonthAssessMapper.selectQgzxMonthAssessById(id);
    }

    /**
     * 查询勤工助学月度考核列表
     *
     * @param qgzxMonthAssess 勤工助学月度考核
     * @return 勤工助学月度考核
     */
    @Override
    public List<QgzxMonthAssess> selectQgzxMonthAssessList(QgzxMonthAssess qgzxMonthAssess) {
        return qgzxMonthAssessMapper.selectQgzxMonthAssessList(qgzxMonthAssess);
    }

    /**
     * 新增勤工助学月度考核
     *
     * @param qgzxMonthAssess 勤工助学月度考核
     * @return 结果
     */
    @Override
    public int insertQgzxMonthAssess(QgzxMonthAssess qgzxMonthAssess) {
        return qgzxMonthAssessMapper.insertQgzxMonthAssess(qgzxMonthAssess);
    }

    /**
     * 修改勤工助学月度考核
     *
     * @param qgzxMonthAssess 勤工助学月度考核
     * @return 结果
     */
    @Override
    public int updateQgzxMonthAssess(QgzxMonthAssess qgzxMonthAssess) {
        return qgzxMonthAssessMapper.updateQgzxMonthAssess(qgzxMonthAssess);
    }

    /**
     * 批量删除勤工助学月度考核
     *
     * @param ids 需要删除的勤工助学月度考核主键
     * @return 结果
     */
    @Override
    public int deleteQgzxMonthAssessByIds(Long[] ids) {
        return qgzxMonthAssessMapper.deleteQgzxMonthAssessByIds(ids);
    }

    /**
     * 删除勤工助学月度考核信息
     *
     * @param id 勤工助学月度考核主键
     * @return 结果
     */
    @Override
    public int deleteQgzxMonthAssessById(Long id) {
        return qgzxMonthAssessMapper.deleteQgzxMonthAssessById(id);
    }
}
