package com.jeeplus.test.projectRecords.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeeplus.sys.utils.UserUtils;
import com.jeeplus.test.projectRecords.domain.Project;
import com.jeeplus.test.projectRecords.mapper.ProjectMapper;
import com.jeeplus.test.projectRecords.service.dto.ProjectDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;

@Service
@Transactional
public class ProjectService extends ServiceImpl<ProjectMapper, Project> {

    @Autowired
    private ProjectMapper projectMapper;

    public IPage<Project> selectPage(Page<Project> page, QueryWrapper queryWrapper){
        queryWrapper.eq ("pr.del_flag", 0 ); // 排除已经删除
        return projectMapper.findList(page, queryWrapper);
    }

    /**
     * 通过项目id查询项目信息
     * @param projectId
     * @return
     */
    public Project selectByProjectId(String projectId){
        Project project = projectMapper.selectByProjectId(projectId);
        if(project.getProjectHead().equals(UserUtils.getCurrentUserDTO().getId())){
            project.setPermissionFlag(true);
        }else{
            project.setPermissionFlag(false);
        }
        return project;
    }

    /**
     * 用于将ProjectDTO类中的DateUi属性转换为时间格式存储到Project类中的Date属性中
     * @param project
     * @param projectDTO
     * @return
     */
    public Project projectDtoToDate(Project project, ProjectDTO projectDTO){
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
        try {
            if(StrUtil.isNotEmpty(projectDTO.getEvaluationBaseDateUi())){
                project.setEvaluationBaseDate(sdf.parse(projectDTO.getEvaluationBaseDateUi()));
            }

            if(StrUtil.isNotEmpty(projectDTO.getEvaluationReportDateUi())){
                project.setEvaluationReportDate(sdf.parse(projectDTO.getEvaluationReportDateUi()));
            }

            if(StrUtil.isNotEmpty(projectDTO.getInvoiceDateUi())){
                project.setInvoiceDate(sdf.parse(projectDTO.getInvoiceDateUi()));
            }

            if(StrUtil.isNotEmpty(projectDTO.getReimbursementDateUi())){
                project.setReimbursementDate(sdf.parse(projectDTO.getReimbursementDateUi()));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return project;
    }

    /**
     * mp的update()方法，如果将参数设为null的，mp会默认不修改的
     * 所以通过这种方式，使用wrapper将你要设置为null的手动set为null，这样update()方法就会按你的想法执行了
     * @param project
     * @return
     */
    public LambdaUpdateWrapper getLambdaUpdateWrapper(ProjectDTO project){
        LambdaUpdateWrapper<Project> wrapper = new LambdaUpdateWrapper<Project>();

        if(StrUtil.isNotEmpty(project.getId())){
            wrapper.eq(Project::getId,project.getId());
        }

        if(StrUtil.isEmpty(project.getEvaluationBaseDateUi())){
            wrapper.set(Project::getEvaluationBaseDate, null);
        }

        if(StrUtil.isEmpty(project.getEvaluationReportDateUi())){
            wrapper.set(Project::getEvaluationReportDate, null);
        }

        if(StrUtil.isEmpty(project.getInvoiceDateUi())){
            wrapper.set(Project::getInvoiceDate, null);
        }

        if(StrUtil.isEmpty(project.getReimbursementDateUi())){
            wrapper.set(Project::getReimbursementDate, null);
        }

        return wrapper;
    }

    /**
     * 根据文号查询项目信息
     * @param documentNum
     * @return
     */
    public Project selectByDocumentNum(String documentNum){
        return projectMapper.selectByDocumentNum(documentNum);
    }
}
