package org.dromara.mp.service.impl;

import cn.hutool.core.date.DateTime;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.satoken.utils.MpLoginHelper;
import org.springframework.stereotype.Service;
import org.dromara.mp.domain.bo.UserWorkCheckinBo;
import org.dromara.mp.domain.vo.UserWorkCheckinVo;
import org.dromara.mp.domain.UserWorkCheckin;
import org.dromara.mp.mapper.UserWorkCheckinMapper;
import org.dromara.mp.service.IUserWorkCheckinService;

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

/**
 * 自雇者打卡记录Service业务层处理
 *
 * @author LionLi
 * @date 2024-04-03
 */
@RequiredArgsConstructor
@Service
public class UserWorkCheckinServiceImpl implements IUserWorkCheckinService {

    private final UserWorkCheckinMapper baseMapper;

    /**
     * 查询自雇者打卡记录
     */
    @Override
    public UserWorkCheckinVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询自雇者打卡记录列表
     */
    @Override
    public TableDataInfo<UserWorkCheckinVo> queryPageList(UserWorkCheckinBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<UserWorkCheckin> lqw = buildQueryWrapper(bo);
        Page<UserWorkCheckinVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询自雇者打卡记录列表
     */
    @Override
    public List<UserWorkCheckinVo> queryList(UserWorkCheckinBo bo) {
        LambdaQueryWrapper<UserWorkCheckin> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<UserWorkCheckin> buildQueryWrapper(UserWorkCheckinBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<UserWorkCheckin> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getRevision() != null, UserWorkCheckin::getRevision, bo.getRevision());
        lqw.eq(StringUtils.isNotBlank(bo.getCreatedBy()), UserWorkCheckin::getCreatedBy, bo.getCreatedBy());
        lqw.eq(bo.getCreatedTime() != null, UserWorkCheckin::getCreatedTime, bo.getCreatedTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdatedBy()), UserWorkCheckin::getUpdatedBy, bo.getUpdatedBy());
        lqw.eq(bo.getProId() != null, UserWorkCheckin::getProId, bo.getProId());
        lqw.eq(StringUtils.isNotBlank(bo.getProNum()), UserWorkCheckin::getProNum, bo.getProNum());
        lqw.eq(bo.getUpdatedTime() != null, UserWorkCheckin::getUpdatedTime, bo.getUpdatedTime());
        lqw.eq(bo.getUserWorkId() != null, UserWorkCheckin::getUserWorkId, bo.getUserWorkId());
        lqw.eq(StringUtils.isNotBlank(bo.getAddressInfo()), UserWorkCheckin::getAddressInfo, bo.getAddressInfo());
        lqw.eq(StringUtils.isNotBlank(bo.getLongi()), UserWorkCheckin::getLongi, bo.getLongi());
        lqw.eq(StringUtils.isNotBlank(bo.getLati()), UserWorkCheckin::getLati, bo.getLati());
        lqw.orderByDesc(UserWorkCheckin::getCreatedTime);
        return lqw;
    }

    /**
     * 新增自雇者打卡记录
     */
    @Override
    public Boolean insertByBo(UserWorkCheckinBo bo) {
        UserWorkCheckin add = MapstructUtils.convert(bo, UserWorkCheckin.class);
        add.setUserWorkId(MpLoginHelper.getUserId());
        add.setCreatedTime(new Date());
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改自雇者打卡记录
     */
    @Override
    public Boolean updateByBo(UserWorkCheckinBo bo) {
        UserWorkCheckin update = MapstructUtils.convert(bo, UserWorkCheckin.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 批量删除自雇者打卡记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
