package com.ruoyi.bgt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.bgt.bo.BgtProjectRecruitQueryBo;
import com.ruoyi.bgt.domain.BgtProjectRecruit;
import com.ruoyi.bgt.domain.dto.BgtProjectRecruitApplyQueryDTO;
import com.ruoyi.bgt.domain.dto.BgtProjectRecruitQueryDTO;
import com.ruoyi.bgt.domain.vo.BgtProjectRecruitDetailVO;
import com.ruoyi.bgt.domain.vo.BgtProjectRecruitVO;
import com.ruoyi.bgt.mapper.BgtProjectRecruitMapper;
import com.ruoyi.bgt.service.IBgtProjectRecruitApplyService;
import com.ruoyi.bgt.service.IBgtProjectRecruitService;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.RecruitApplyStatus;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.fbs.domain.FbsProjectTask;
import com.ruoyi.fbs.service.IFbsProjectTaskService;
import com.ruoyi.wgz.bo.req.WgzAppRegisteredProjectReq;
import com.ruoyi.wgz.bo.res.WgzAppCancelRegistrationProjectDetailsRes;
import com.ruoyi.wgz.bo.res.WgzAppProjectDetailsRes;
import com.ruoyi.wgz.bo.res.WgzAppRegisteredProjectRes;
import com.ruoyi.wgz.bo.res.WgzAppUserDailyRecordRes;
import com.ruoyi.wgz.bo.rests.WgzAppCancelRegistrationProjectDetailsTwo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.*;

/**
 * 包工头招工Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-17
 */
@Service
public class BgtProjectRecruitServiceImpl extends ServicePlusImpl<BgtProjectRecruitMapper, BgtProjectRecruit> implements IBgtProjectRecruitService {

	@Autowired
	private IFbsProjectTaskService fbsProjectTaskService;

	@Autowired
	private IBgtProjectRecruitApplyService bgtProjectRecruitApplyService;


    @Override
    public BgtProjectRecruit queryById(Long id){
        return getById(id);
    }

    @Override
    public TableDataInfo<BgtProjectRecruit> queryPageList(BgtProjectRecruitQueryBo bo) {
        Page<BgtProjectRecruit> result = page(PageUtils.buildPage(), buildQueryWrapper(bo));
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<BgtProjectRecruit> queryList(BgtProjectRecruitQueryBo bo) {
        return list(buildQueryWrapper(bo));
    }

    private LambdaQueryWrapper<BgtProjectRecruit> buildQueryWrapper(BgtProjectRecruitQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BgtProjectRecruit> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getProjectId() != null, BgtProjectRecruit::getProjectId, bo.getProjectId());
        lqw.eq(bo.getSectionId() != null, BgtProjectRecruit::getSectionId, bo.getSectionId());
        lqw.eq(bo.getSubId() != null, BgtProjectRecruit::getSubId, bo.getSubId());
        lqw.eq(bo.getTaskId() != null, BgtProjectRecruit::getTaskId, bo.getTaskId());
        lqw.like(StrUtil.isNotBlank(bo.getRecruitName()), BgtProjectRecruit::getRecruitName, bo.getRecruitName());
        lqw.eq(StrUtil.isNotBlank(bo.getRecruitAddress()), BgtProjectRecruit::getRecruitAddress, bo.getRecruitAddress());
        lqw.eq(bo.getRecruitAmount() != null, BgtProjectRecruit::getRecruitAmount, bo.getRecruitAmount());
        lqw.eq(bo.getRecruitStaffNum() != null, BgtProjectRecruit::getRecruitStaffNum, bo.getRecruitStaffNum());
        lqw.eq(StrUtil.isNotBlank(bo.getRecruitBeginTime()), BgtProjectRecruit::getRecruitBeginTime, bo.getRecruitBeginTime());
        lqw.eq(StrUtil.isNotBlank(bo.getRecruitContactPerson()), BgtProjectRecruit::getRecruitContactPerson, bo.getRecruitContactPerson());
        lqw.eq(StrUtil.isNotBlank(bo.getRecruitContactPhone()), BgtProjectRecruit::getRecruitContactPhone, bo.getRecruitContactPhone());
        lqw.eq(StrUtil.isNotBlank(bo.getSubDescribe()), BgtProjectRecruit::getSubDescribe, bo.getSubDescribe());
        lqw.eq(StrUtil.isNotBlank(bo.getRecruitRequirement()), BgtProjectRecruit::getRecruitRequirement, bo.getRecruitRequirement());
        return lqw;
    }

    @Override
    public Boolean insert(BgtProjectRecruit bo) {
        BgtProjectRecruit add = BeanUtil.toBean(bo, BgtProjectRecruit.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean update(BgtProjectRecruit bo) {
        BgtProjectRecruit update = BeanUtil.toBean(bo, BgtProjectRecruit.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(BgtProjectRecruit entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

	@Override
	public TableDataInfo<BgtProjectRecruitVO> appQueryPageList(BgtProjectRecruitQueryDTO dto) {
		Page<BgtProjectRecruitQueryDTO> queryDTOPage = new Page<>();
		queryDTOPage.setCurrent(dto.getPageNum());
		queryDTOPage.setSize(dto.getPageSize());
		Page<BgtProjectRecruitVO> queryVOPage = baseMapper.appQueryPageList(queryDTOPage, dto);
		for(BgtProjectRecruitVO vo : queryVOPage.getRecords()) {
			BgtProjectRecruitApplyQueryDTO bgtProjectRecruitApplyQueryDTO = new BgtProjectRecruitApplyQueryDTO();
			bgtProjectRecruitApplyQueryDTO.setRecruitIds(Collections.singletonList(vo.getId()));
			bgtProjectRecruitApplyQueryDTO.setStatus(dto.getStatus());
			vo.setApplylist(bgtProjectRecruitApplyService.appQueryList(bgtProjectRecruitApplyQueryDTO));
		}
		return PageUtils.buildDataInfo(queryVOPage);
	}

	@Override
	public BgtProjectRecruitDetailVO appQueryById(Long id) {
		BgtProjectRecruit recruit = getById(id);
		BgtProjectRecruitDetailVO vo = BeanUtil.toBean(recruit, BgtProjectRecruitDetailVO.class);
		FbsProjectTask task = fbsProjectTaskService.getById(vo.getTaskId());
		vo.setTaskName(task.getTaskName());
		BgtProjectRecruitApplyQueryDTO bgtProjectRecruitApplyQueryDTO = new BgtProjectRecruitApplyQueryDTO();
		bgtProjectRecruitApplyQueryDTO.setRecruitIds(Collections.singletonList(vo.getId()));
		bgtProjectRecruitApplyQueryDTO.setStatus(Collections.singletonList(RecruitApplyStatus.SIGN_UP.getCode()));
		vo.setApplylist(bgtProjectRecruitApplyService.appQueryList(bgtProjectRecruitApplyQueryDTO));
		return vo;
	}

	/**
	 * LC-APP相关
	 * =================================================================================================================
	 * =================================================================================================================
	 * =================================================================================================================
	 */

	@Override
	public BgtProjectRecruit getAppById(Long id) {
		return baseMapper.selectById(id);
	}

	@Override
	public WgzAppProjectDetailsRes userProjectDetails(Long id) {
		return baseMapper.userProjectDetails(id);
	}

	@Override
	public TableDataInfo<WgzAppRegisteredProjectRes> userRegisteredProject(WgzAppRegisteredProjectReq req) {
		Long userId = SecurityUtils.getAppUserId();
		Page<WgzAppRegisteredProjectReq> pe = new Page<>();
		pe.setCurrent(req.getPageNum());
		pe.setSize(req.getPageSize());
		return PageUtils.buildDataInfo(baseMapper.userRegisteredProject(pe, userId));
	}

	@Override
	public WgzAppCancelRegistrationProjectDetailsRes userCancelRegistrationProjectDetails(Long recruitId) {
		//1、获取项目基础信息
		WgzAppProjectDetailsRes wgzAppProjectDetailsRes = baseMapper.userProjectDetails(recruitId);
		//2、获取当前项目下的成员
		List<WgzAppCancelRegistrationProjectDetailsTwo> wgzUser = baseMapper.GetsTheMembersUnderTheCurrentProject(recruitId);
		//3、数据组装
		WgzAppCancelRegistrationProjectDetailsRes res = new WgzAppCancelRegistrationProjectDetailsRes();
		BeanUtils.copyProperties(wgzAppProjectDetailsRes,res);
		res.setNumberOfRegistered(wgzUser.size());
		res.setUserList(wgzUser);
		return res;
	}

}
