package thyyxxk.webserver.service.lc.project;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.dao.his.lc.project.LcProjectAliasDao;
import thyyxxk.webserver.dao.his.lc.project.LcProjectDao;
import thyyxxk.webserver.entity.lc.company.LcCompany;
import thyyxxk.webserver.entity.lc.company.LcCompanyParam;
import thyyxxk.webserver.entity.lc.project.LcProject;
import thyyxxk.webserver.entity.lc.project.LcProjectAlias;
import thyyxxk.webserver.entity.lc.project.LcProjectParam;

import java.util.List;
import java.util.UUID;

@Slf4j
@Service
public class LcProjectService {

    @Autowired
    private LcProjectDao dao;

    @Autowired
    private LcProjectAliasDao lcProjectAliasDao;

    @Autowired
    private LcProjectAliasDao lcProjectAliasDaodao;

    public LcProjectParam getLcProjectById(String id) {
        LcProjectParam lcProject = (LcProjectParam)dao.selectById(id);
        LambdaQueryWrapper<LcProjectAlias> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LcProjectAlias::getLcProjectId, id);
        List<LcProjectAlias> lcProjectAliasList = lcProjectAliasDao.selectList(queryWrapper);
        lcProject.setLcProjectAliasList(lcProjectAliasList);
        return lcProject;
    }

    public int insertLcProject(LcProjectParam lcProjectParam) {
        lcProjectParam.setId(UUID.randomUUID().toString().replace("-", ""));
        int insertProject = dao.insert(lcProjectParam);
        if (insertProject > 0) {
            for(LcProjectAlias lcProjectAlias : lcProjectParam.getLcProjectAliasList()) {
                lcProjectAlias.setId(UUID.randomUUID().toString().replace("-", ""));
                lcProjectAliasDaodao.insert(lcProjectAlias);
            }
        }
        return insertProject;
    }

    public Page<LcProject> getLcProjectList(LcProjectParam lcProjectParam) {
        Page<LcProject> page = new Page<>(lcProjectParam.getPageNumber(), lcProjectParam.getPageSize());
        LambdaQueryWrapper<LcProject> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(lcProjectParam.getProjectCode())) {
            queryWrapper.like(LcProject::getProjectCode, lcProjectParam.getProjectCode());
        }
        if (StringUtils.isNotBlank(lcProjectParam.getProjectName())) {
            queryWrapper.like(LcProject::getProjectName, lcProjectParam.getProjectName());
        }
        if (StringUtils.isNotBlank(lcProjectParam.getProjectType())) {
            queryWrapper.like(LcProject::getProjectType, lcProjectParam.getProjectType());
        }
        if (StringUtils.isNotBlank(lcProjectParam.getDelFlag())) {
            queryWrapper.eq(LcProject::getDelFlag, lcProjectParam.getDelFlag());
        }

        return dao.selectPage(page, queryWrapper);
    }
}
