package org.dromara.mp.service.impl;

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.springframework.stereotype.Service;
import org.dromara.mp.domain.bo.UserWorkerBo;
import org.dromara.mp.domain.vo.UserWorkerVo;
import org.dromara.mp.domain.UserWorker;
import org.dromara.mp.mapper.UserWorkerMapper;
import org.dromara.mp.service.IUserWorkerService;

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

/**
 * 自雇者Service业务层处理
 *
 * @author LionLi
 * @date 2024-03-28
 */
@RequiredArgsConstructor
@Service
public class UserWorkerServiceImpl implements IUserWorkerService {

    private final UserWorkerMapper baseMapper;

    /**
     * 查询自雇者
     */
    @Override
    public UserWorkerVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询自雇者列表
     */
    @Override
    public TableDataInfo<UserWorkerVo> queryPageList(UserWorkerBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<UserWorker> lqw = buildQueryWrapper(bo);
        Page<UserWorkerVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }


    /**
     * 查询自雇者列表
     */
    @Override
    public List<UserWorkerVo> queryList(UserWorkerBo bo) {
        LambdaQueryWrapper<UserWorker> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<UserWorker> buildQueryWrapper(UserWorkerBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<UserWorker> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getOpenId()), UserWorker::getOpenId, bo.getOpenId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), UserWorker::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), UserWorker::getPhone, bo.getPhone());
        lqw.eq(bo.getIsAuthentication() != null, UserWorker::getIsAuthentication, bo.getIsAuthentication());
        lqw.eq(StringUtils.isNotBlank(bo.getCardId()), UserWorker::getCardId, bo.getCardId());
        lqw.eq(StringUtils.isNotBlank(bo.getCardPositive()), UserWorker::getCardPositive, bo.getCardPositive());
        lqw.eq(StringUtils.isNotBlank(bo.getCardBack()), UserWorker::getCardBack, bo.getCardBack());
        lqw.eq(StringUtils.isNotBlank(bo.getBankType()), UserWorker::getBankType, bo.getBankType());
        lqw.eq(StringUtils.isNotBlank(bo.getBankCard()), UserWorker::getBankCard, bo.getBankCard());
        lqw.eq(bo.getIsAuth() != null, UserWorker::getIsAuth, bo.getIsAuth());
        lqw.eq(bo.getIsSign() != null, UserWorker::getIsSign, bo.getIsSign());
        lqw.eq(bo.getSignTime() != null, UserWorker::getSignTime, bo.getSignTime());
        lqw.eq(bo.getIsRegister() != null, UserWorker::getIsRegister, bo.getIsRegister());
        lqw.eq(StringUtils.isNotBlank(bo.getPhotoPath()), UserWorker::getPhotoPath, bo.getPhotoPath());
        lqw.eq(StringUtils.isNotBlank(bo.getSex()), UserWorker::getSex, bo.getSex());
        lqw.eq(StringUtils.isNotBlank(bo.getTag()), UserWorker::getTag, bo.getTag());
        lqw.eq(StringUtils.isNotBlank(bo.getUserArea()), UserWorker::getUserArea, bo.getUserArea());
        lqw.like(StringUtils.isNotBlank(bo.getUserAreaName()), UserWorker::getUserAreaName, bo.getUserAreaName());
        lqw.eq(bo.getAddTime() != null, UserWorker::getAddTime, bo.getAddTime());
        lqw.eq(StringUtils.isNotBlank(bo.getAddBy()), UserWorker::getAddBy, bo.getAddBy());
        lqw.eq(StringUtils.isNotBlank(bo.getEditer()), UserWorker::getEditer, bo.getEditer());
        lqw.eq(bo.getEditTime() != null, UserWorker::getEditTime, bo.getEditTime());
        lqw.eq(StringUtils.isNotBlank(bo.getNation()), UserWorker::getNation, bo.getNation());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), UserWorker::getAddress, bo.getAddress());
        lqw.eq(bo.getBirthday() != null, UserWorker::getBirthday, bo.getBirthday());
        lqw.eq(StringUtils.isNotBlank(bo.getIssue()), UserWorker::getIssue, bo.getIssue());
        lqw.eq(bo.getIssueDate() != null, UserWorker::getIssueDate, bo.getIssueDate());
        lqw.eq(StringUtils.isNotBlank(bo.getExpiryDate()), UserWorker::getExpiryDate, bo.getExpiryDate());
        lqw.eq(StringUtils.isNotBlank(bo.getReferrerBy()), UserWorker::getReferrerBy, bo.getReferrerBy());
        lqw.eq(StringUtils.isNotBlank(bo.getAlipayLogonId()), UserWorker::getAlipayLogonId, bo.getAlipayLogonId());
        lqw.eq(bo.getStatus() != null, UserWorker::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增自雇者
     */
    @Override
    public Boolean insertByBo(UserWorkerBo bo) {
        UserWorker add = MapstructUtils.convert(bo, UserWorker.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改自雇者
     */
    @Override
    public Boolean updateByBo(UserWorkerBo bo) {
        UserWorker update = MapstructUtils.convert(bo, UserWorker.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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