package org.dromara.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.domain.dto.StaffManageDTO;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.domain.checkPoint.CheckPointRecord;
import org.dromara.domain.checkPoint.bo.CheckPointRecordBo;
import org.dromara.domain.checkPoint.vo.CheckPointRecordVo;
import org.dromara.mapper.CheckPointRecordMapper;
import org.dromara.service.ICheckPointRecordService;
import org.springframework.stereotype.Service;

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

import static org.dromara.common.core.constant.Constants.TWO;

/**
 * 巡更点记录Service业务层处理
 *
 * @author boman
 * @date 2025-04-11
 */
@RequiredArgsConstructor
@Service
public class CheckPointRecordServiceImpl implements ICheckPointRecordService {

    private final CheckPointRecordMapper baseMapper;

    /**
     * 查询巡更点记录
     *
     * @param checkPointRecordId 主键
     * @return 巡更点记录
     */
    @Override
    public CheckPointRecordVo queryById(Long checkPointRecordId){
        return baseMapper.selectVoById(checkPointRecordId);
    }

    /**
     * 分页查询巡更点记录列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 巡更点记录分页列表
     */
    @Override
    public TableDataInfo<CheckPointRecordVo> queryPageList(CheckPointRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CheckPointRecord> lqw = buildQueryWrapper(bo);
        Page<CheckPointRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的巡更点记录列表
     *
     * @param bo 查询条件
     * @return 巡更点记录列表
     */
    @Override
    public List<CheckPointRecordVo> queryList(CheckPointRecordBo bo) {
        LambdaQueryWrapper<CheckPointRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CheckPointRecord> buildQueryWrapper(CheckPointRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CheckPointRecord> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(CheckPointRecord::getCheckPointRecordId);
        lqw.eq(bo.getCheckPointManageId() != null, CheckPointRecord::getCheckPointManageId, bo.getCheckPointManageId());
        lqw.eq(StringUtils.isNotBlank(bo.getDateTimeNum()), CheckPointRecord::getDateTimeNum, bo.getDateTimeNum());
        lqw.eq(StringUtils.isNotBlank(bo.getCheckType()), CheckPointRecord::getCheckType, bo.getCheckType());
        lqw.eq(bo.getCheckTime() != null, CheckPointRecord::getCheckTime, bo.getCheckTime());
        lqw.eq(StringUtils.isNotBlank(bo.getCheckAddress()), CheckPointRecord::getCheckAddress, bo.getCheckAddress());
        lqw.like(StringUtils.isNotBlank(bo.getUserName()), CheckPointRecord::getUserName, bo.getUserName());
        lqw.eq(bo.getUseId() != null, CheckPointRecord::getUseId, bo.getUseId());
        lqw.like(StringUtils.isNotBlank(bo.getCheckUserName()), CheckPointRecord::getCheckUserName, bo.getCheckUserName());
        lqw.eq(bo.getCheckUseId() != null, CheckPointRecord::getCheckUseId, bo.getCheckUseId());
        lqw.eq(bo.getCheckStaffId() != null, CheckPointRecord::getCheckStaffId, bo.getCheckStaffId());
        lqw.eq(StringUtils.isNotBlank(bo.getCheckUsePhone()), CheckPointRecord::getCheckUsePhone, bo.getCheckUsePhone());
        lqw.eq(StringUtils.isNotBlank(bo.getCheckResult()), CheckPointRecord::getCheckResult, bo.getCheckResult());
        lqw.eq(StringUtils.isNotBlank(bo.getCheckResultDetails()), CheckPointRecord::getCheckResultDetails, bo.getCheckResultDetails());
        lqw.eq(StringUtils.isNotBlank(bo.getCheckResultPhoto()), CheckPointRecord::getCheckResultPhoto, bo.getCheckResultPhoto());
        lqw.eq(bo.getCheckResultTime() != null, CheckPointRecord::getCheckResultTime, bo.getCheckResultTime());
        lqw.eq(StringUtils.isNotBlank(bo.getCheckStatus()), CheckPointRecord::getCheckStatus, bo.getCheckStatus());
        return lqw;
    }

    /**
     * 新增巡更点记录
     *
     * @param bo 巡更点记录
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(CheckPointRecordBo bo) {
        CheckPointRecord add = MapstructUtils.convert(bo, CheckPointRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setCheckPointRecordId(add.getCheckPointRecordId());
        }
        return flag;
    }

    /**
     * 修改巡更点记录
     *
     * @param bo 巡更点记录
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(CheckPointRecordBo bo) {
        StaffManageDTO staffManage = LoginHelper.getLoginUser().getStaffManage();
        bo.setUpdateTime(DateUtils.getNowDate());
        if (staffManage != null){
            bo.setCheckUserName(staffManage.getStaffName());
            bo.setCheckUsePhone(staffManage.getPhoneNumber());
            bo.setCheckStaffId(staffManage.getStaffId());
        }
        CheckPointRecord update = MapstructUtils.convert(bo, CheckPointRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CheckPointRecord entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除巡更点记录信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public R<Map<String, Object>> getCheckPointRecordCount(CheckPointRecordBo bo) {
        Map<String,Object> resultMap = new HashMap<>(3);
        resultMap.put("checkPoint",0);
        resultMap.put("yCheckPoint",0);
        resultMap.put("wCheckPoint",0);
        bo.setCreateTime(DateUtils.getNowDate());
        //查询出今日所有巡更点记录
        List<CheckPointRecordVo> checkPointRecords = queryList(bo);
        if (checkPointRecords != null && !checkPointRecords.isEmpty()){
            //判断巡更时间不为null的代表已经巡更
            resultMap.put("checkPoint",checkPointRecords.size());
            long checkCount = checkPointRecords.stream().filter(e -> TWO.equals(e.getCheckStatus())).count();
            resultMap.put("yCheckPoint",checkCount);
            resultMap.put("wCheckPoint",checkPointRecords.size()-checkCount);
        }
        return R.ok(resultMap);
    }
}
