package com.yccrc.kede.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yccrc.common.core.domain.entity.SysUser;
import com.yccrc.common.utils.PageUtils;
import com.yccrc.common.utils.SecurityUtils;
import com.yccrc.kede.domain.Enterprise;
import com.yccrc.kede.domain.EnterpriseYearInfo;
import com.yccrc.kede.mapper.EnterpriseYearInfoMapper;
import com.yccrc.kede.service.IEnterpriseService;
import com.yccrc.kede.service.IEnterpriseYearInfoService;
import com.yccrc.kede.vo.ProjectQueryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author pengzhiqiang
 * @description 针对表【enterprise_year_info】的数据库操作Service实现
 * @createDate 2023-07-26 14:11:03
 */
@Slf4j
@Service
public class EnterpriseYearInfoServiceImpl extends ServiceImpl<EnterpriseYearInfoMapper, EnterpriseYearInfo>
        implements IEnterpriseYearInfoService {

    @Autowired
    private IEnterpriseService enterpriseService;

    @Override
    public Boolean saveYearProject(EnterpriseYearInfo yearInfo) {
        try {
            SysUser user = SecurityUtils.getLoginUser().getUser();
            Enterprise enterprise = enterpriseService.getById(user.getScopeId());
            long count = this.count(new LambdaQueryWrapper<EnterpriseYearInfo>()
                    .eq(EnterpriseYearInfo::getEntId, user.getScopeId())
                    .eq(EnterpriseYearInfo::getProjectYear, yearInfo.getProjectYear())
            );
            if (count > 0) {
                return false;
            }
            String entName = enterprise != null ? enterprise.getEntName() : null;
            yearInfo.setEntId(Integer.valueOf(user.getScopeId()));
            yearInfo.setEntName(entName);
            this.save(yearInfo);
        } catch (Exception e) {
            log.error("saveYearProject:{}", e.getMessage());
            return false;
        }
        return true;
    }

    @Override
    public Page<EnterpriseYearInfo> selectList(ProjectQueryVO project) {
        QueryWrapper<EnterpriseYearInfo> queryWrapper = new QueryWrapper<EnterpriseYearInfo>()
                .orderByDesc("a.id");
        SysUser user = SecurityUtils.getLoginUser().getUser();
        switch (user.getUserType()) {
            case ADMIN:
                // 查看全部
                break;
            case ENT_ADMIN:
            case ENT_ADMIN_TECH:
            case ENT_ADMIN_FIN:
            case ENT_TECH:
            case ENT_FIN:
                queryWrapper.eq("ent_id", user.getScopeId());
                break;
            default:
                break;
        }
        Page<EnterpriseYearInfo> page = baseMapper.selectPage(PageUtils.initPage(), queryWrapper);
        return page;
    }

}




