package com.bonus.sgzb.material.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.bonus.sgzb.common.core.utils.DateUtils;
import com.bonus.sgzb.common.core.utils.StringHelper;
import com.bonus.sgzb.base.api.domain.BackApplyInfo;
import com.bonus.sgzb.material.domain.TmTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bonus.sgzb.material.mapper.TaskMapper;
import com.bonus.sgzb.material.service.TaskService;

/**
 * 任务tm_taskService业务层处理
 * 
 * @author bonus
 * @date 2023-12-13
 */
@Service
public class TaskServiceImpl implements TaskService
{
    @Autowired
    private TaskMapper tmTaskMapper;

    /**
     * 查询任务tm_task
     * 
     * @param taskId 任务tm_task主键
     * @return 任务tm_task
     */
    @Override
    public TmTask selectTmTaskByTaskId(Long taskId)
    {
        return tmTaskMapper.selectTmTaskByTaskId(taskId);
    }

    /**
     * 查询任务tm_task列表
     * 
     * @param tmTask 任务tm_task
     * @return 任务tm_task
     */
    @Override
    public List<TmTask> selectTmTaskList(TmTask tmTask)
    {
        return tmTaskMapper.selectTmTaskList(tmTask);
    }

    /**
     * 新增任务tm_task
     * 
     * @param tmTask 任务tm_task
     * @return 结果
     */
    @Override
    public int insertTmTask(TmTask tmTask)
    {
        tmTask.setCreateTime(DateUtils.getNowDate());
        return tmTaskMapper.insertTmTask(tmTask);
    }

    /**
     * 修改任务tm_task
     * 
     * @param tmTask 任务tm_task
     * @return 结果
     */
    @Override
    public int updateTmTask(TmTask tmTask)
    {
        tmTask.setUpdateTime(DateUtils.getNowDate());
        return tmTaskMapper.updateTmTask(tmTask);
    }

    /**
     * 批量删除任务tm_task
     * 
     * @param taskIds 需要删除的任务tm_task主键
     * @return 结果
     */
    @Override
    public int deleteTmTaskByTaskIds(Long[] taskIds)
    {
        return tmTaskMapper.deleteTmTaskByTaskIds(taskIds);
    }

    /**
     * 删除任务tm_task信息
     * 
     * @param taskId 任务tm_task主键
     * @return 结果
     */
    @Override
    public int deleteTmTaskByTaskId(Long taskId)
    {
        return tmTaskMapper.deleteTmTaskByTaskId(taskId);
    }

    @Override
    public String genderLeaseCode() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date nowDate = DateUtils.getNowDate();
        String format = dateFormat.format(nowDate);
        String result = format.replace("-", "");
        int num = tmTaskMapper.selectTaskNumByMonth(nowDate,29);
        num = num + 1;
        String code = "";
        if (num > 9 && num < 100) {
            code = "L" + result + "-00" + num;
        } else if (num > 99 && num < 1000) {
            code = "L" + result + "-0" + num;
        } else {
            code = "L" + result + "-000" + num;
        }
        return code;
    }

    @Override
    public int insertSelective(TmTask bean) {
        return tmTaskMapper.insertSelective(bean);
    }

    @Override
    public int insertAgreement(TmTask task) {
        return tmTaskMapper.insertAgreement(task);
    }

    @Override
    public String genderBackCode() {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
            Date nowDate = DateUtils.getNowDate();
            String format = dateFormat.format(nowDate);
            String taskNum = tmTaskMapper.selectTaskNumByMonths(nowDate, 36);
        if (StringHelper.isNotEmpty(taskNum)){
            // 将字符串转换为整数
            int num = Integer.parseInt(taskNum);
            // 执行加一操作
            num++;
            // 将结果转换回字符串格式，并确保结果是四位数，不足四位则在前面补0
            taskNum = String.format("%04d", num);
        }else {
            taskNum = "0001";
        }
            String code = "T" + format + "-" + taskNum;
            return code;
        }

    @Override
    public int insertTmTaskByBackInfo(BackApplyInfo backApplyInfo) {
       return tmTaskMapper.insertTmTaskByBackInfo(backApplyInfo);
    }

    @Override
    public int insertAgreementByBackInfo(BackApplyInfo backApplyInfo) {
        return tmTaskMapper.insertAgreementByBackInfo(backApplyInfo);
    }
}
