package com.yunyan.business.service.impl;

import java.util.List;

import cn.hutool.core.date.DateUtil;
import com.yunyan.common.exception.ServiceException;
import com.yunyan.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yunyan.business.mapper.FillDateMapper;
import com.yunyan.business.domain.FillDate;
import com.yunyan.business.service.IFillDateService;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.Builder;
import com.yunyan.common.core.domain.AjaxResultVo;
import org.springframework.beans.BeanUtils;
import com.yunyan.common.core.domain.model.LoginUser;
import com.yunyan.common.utils.SecurityUtils;
import com.yunyan.common.enums.DataContentType;
import lombok.extern.slf4j.Slf4j;
import java.util.Date;
import com.yunyan.common.annotation.DataScope;
import org.springframework.transaction.annotation.Transactional;

/**
 * 报告期限设置Service业务层处理
 *
 * @author yunyan
 * @date 2023-07-27
 */
@Slf4j
@Service
public class FillDateServiceImpl extends ServiceImpl<FillDateMapper, FillDate> implements IFillDateService
{
    @Autowired
    private FillDateMapper fillDateMapper;

    /**
     * 查询报告期限设置列表
     *
     * @param fillDate 报告期限设置
     * @return 报告期限设置
     */
    //@DataScope(deptAlias = "t", userAlias = "t")
    @Override
    public List<FillDate> selectFillDateList(FillDate fillDate)
    {
        return fillDateMapper.selectFillDateList(fillDate);
    }

    /**
    * 新增
    * @param
    * @return
    */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResultVo add(FillDate dto) {
        FillDate entity = dto;

        /*List<Long> deptIdList = SecurityUtils.getDeptIdList();
        if(deptIdList.isEmpty()) {
            return AjaxResultVo.error("当前用户无归属单位，不能添加");
        }*/

        if(null == dto.getDeptIdList() || dto.getDeptIdList().isEmpty()) {
            throw new ServiceException("部门ID不能为空");
        }

        for (Long deptId : dto.getDeptIdList()) {
            List<FillDate> list = this.lambdaQuery().in(FillDate::getDeptId, deptId).eq(FillDate::getYear, dto.getYear()).list();
            if(!list.isEmpty()) {
                return AjaxResultVo.error("当前单位已经设置填报日期，无需再次设置");
            }
            entity.setDeptId(deptId);

            this.save(entity);
        }


        return AjaxResultVo.success();
    }

    /**
     * 编辑
     * @param
     * @return
     */
    @Override
    public AjaxResultVo edit(FillDate dto) {

        FillDate entity = this.getById(dto.getId());
        if(null == entity) {
            return AjaxResultVo.error("非法操作");
        }

        entity = new FillDate();
        BeanUtils.copyProperties(dto, entity);

        this.updateById(entity);
        return AjaxResultVo.success();
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @Override
    public AjaxResultVo info(Long id) {
        FillDate entity = this.getById(id);
        if(null == entity) {
            return AjaxResultVo.error("数据查询异常~");
        }

        return AjaxResultVo.success(entity);
    }

    @Override
    public FillDate getFillDateByYearType(Integer type, String year, Long deptId) {
        /*FillDate fillDate = lambdaQuery().eq(FillDate::getType, type).eq(FillDate::getYear, year).eq(FillDate::getDeptId, deptId).apply(" rownum <= 1").orderByDesc(FillDate::getId).one();
        return fillDate;*/
        return null;
    }

    @Override
    public int getFillDateValidNum(Integer type, Long deptId, String year) {
        //Integer type = 1;
        //int num = lambdaQuery().eq(FillDate::getType, type).eq(FillDate::getYear, year).exists(String.format("select 1 from sys_dept where tb_fill_date.dept_id = parent_id AND dept_id = %s", deptId))
        int num = lambdaQuery().eq(FillDate::getType, type).eq(FillDate::getYear, year).eq(FillDate::getDeptId, deptId)
                .le(FillDate::getFillBeginTime, new Date()).ge(FillDate::getFillEndTime, new Date()).count();

        return num;
    }
}
