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.BgtWageApplicationQueryBo;
import com.ruoyi.bgt.domain.BgtWageApplication;
import com.ruoyi.bgt.domain.dto.BgtWageApplicationQueryDTO;
import com.ruoyi.bgt.domain.vo.BgtWageApplicationDetailVO;
import com.ruoyi.bgt.domain.vo.BgtWageApplicationListVO;
import com.ruoyi.bgt.domain.vo.BgtWageApplicationVO;
import com.ruoyi.bgt.mapper.BgtWageApplicationMapper;
import com.ruoyi.bgt.service.IBgtUserService;
import com.ruoyi.bgt.service.IBgtWageApplicationService;
import com.ruoyi.common.core.domain.entity.BgtUser;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.fbs.domain.FbsProjectTask;
import com.ruoyi.fbs.service.IFbsProjectTaskService;
import com.ruoyi.zbf.domain.ZbfProject;
import com.ruoyi.zbf.service.IZbfProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 包工头工资申请Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-20
 */
@Service
public class BgtWageApplicationServiceImpl extends ServicePlusImpl<BgtWageApplicationMapper, BgtWageApplication> implements IBgtWageApplicationService {

	@Autowired
	private IZbfProjectService projectService;

	@Autowired
	private IFbsProjectTaskService taskService;

	@Autowired
	private IBgtUserService bgtUserService;


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

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

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

    private LambdaQueryWrapper<BgtWageApplication> buildQueryWrapper(BgtWageApplicationQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BgtWageApplication> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getProjectId() != null, BgtWageApplication::getProjectId, bo.getProjectId());
        lqw.eq(bo.getSectionId() != null, BgtWageApplication::getSectionId, bo.getSectionId());
        lqw.eq(bo.getSubId() != null, BgtWageApplication::getSubId, bo.getSubId());
        lqw.eq(bo.getTaskId() != null, BgtWageApplication::getTaskId, bo.getTaskId());
        lqw.eq(bo.getUserId() != null, BgtWageApplication::getUserId, bo.getUserId());
        lqw.eq(bo.getApplicantAmount() != null, BgtWageApplication::getApplicantAmount, bo.getApplicantAmount());
        lqw.eq(StrUtil.isNotBlank(bo.getEmploymentSituation()), BgtWageApplication::getEmploymentSituation, bo.getEmploymentSituation());
        lqw.eq(StrUtil.isNotBlank(bo.getCompletionSituation()), BgtWageApplication::getCompletionSituation, bo.getCompletionSituation());
        lqw.eq(StrUtil.isNotBlank(bo.getTaskSituation()), BgtWageApplication::getTaskSituation, bo.getTaskSituation());
        lqw.eq(StrUtil.isNotBlank(bo.getAuditStatus()), BgtWageApplication::getAuditStatus, bo.getAuditStatus());
        lqw.eq(bo.getReviewerId() != null, BgtWageApplication::getReviewerId, bo.getReviewerId());
        return lqw;
    }

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

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

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

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

	@Override
	public TableDataInfo<BgtWageApplicationVO> appQueryPageList(BgtWageApplicationQueryDTO dto) {
		Page<BgtWageApplicationQueryDTO> queryDTOPage = new Page<>();
		queryDTOPage.setCurrent(dto.getPageNum());
		queryDTOPage.setSize(dto.getPageSize());
		Page<BgtWageApplicationVO> queryVOPage = baseMapper.appQueryPageList(queryDTOPage, dto);
		return PageUtils.buildDataInfo(queryVOPage);
	}

	@Override
	public BgtWageApplicationDetailVO appQueryById(Long id) {
		BgtWageApplication application = getById(id);
		BgtWageApplicationDetailVO detailVO = BeanUtil.copyProperties(application, BgtWageApplicationDetailVO.class);
		//项目
		ZbfProject project = projectService.getById(application.getProjectId());
		detailVO.setProjectName(project.getProjectName());
		//任务
		FbsProjectTask task = taskService.getById(application.getTaskId());
		detailVO.setTaskAddress(task.getTaskAddress());
		detailVO.setTaskName(task.getTaskName());
		detailVO.setTaskStaffNum(task.getTaskStaffNum());
		//人员
		BgtUser bgtUser = bgtUserService.selectUserByUserId(application.getUserId());
		detailVO.setBank(bgtUser.getBank());
		detailVO.setCardNo(bgtUser.getCardNo());
		detailVO.setPhone(bgtUser.getPhone());
		return detailVO;
	}

	@Override
	public List<BgtWageApplicationListVO> appQueryList(BgtWageApplicationQueryDTO dto) {
		return baseMapper.appQueryList(dto);
	}
}
