package org.dromara.payment.worker.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.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.payment.worker.domain.bo.UserWorkerBo;
import org.dromara.payment.worker.domain.vo.UserWorkerVo;
import org.dromara.payment.worker.domain.UserWorker;
import org.dromara.payment.worker.mapper.UserWorkerMapper;
import org.dromara.payment.worker.service.IUserWorkerService;

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

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

    private final UserWorkerMapper baseMapper;

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

    /**
     * 查询自雇者
     *
     * @param name
     */
    @Override
    public UserWorkerVo queryByName(String name) {
        return baseMapper.selectVoOne(Wrappers.<UserWorker>query().eq("name", name));
    }

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

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

    private QueryWrapper<UserWorker> buildQueryWrapper(UserWorkerBo bo) {
        Map<String, Object> params = bo.getParams();
        QueryWrapper<UserWorker> lqw = new QueryWrapper<>();
        lqw.eq(StringUtils.isNotBlank(bo.getOpenId()), "open_id", bo.getOpenId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), "name", bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), "phone", bo.getPhone());
        lqw.eq(bo.getIsAuthentication() != null, "is_authentication", bo.getIsAuthentication());
        lqw.eq(StringUtils.isNotBlank(bo.getCardId()), "card_id", bo.getCardId());
        lqw.eq(StringUtils.isNotBlank(bo.getCardPositive()), "card_positive", bo.getCardPositive());
        lqw.eq(StringUtils.isNotBlank(bo.getCardBack()), "card_back", bo.getCardBack());
        lqw.eq(StringUtils.isNotBlank(bo.getBankType()), "bank_type", bo.getBankType());
        lqw.eq(StringUtils.isNotBlank(bo.getBankCard()), "bank_card", bo.getBankCard());
        lqw.eq(bo.getIsAuth() != null, "is_auth", bo.getIsAuth());
        lqw.eq(bo.getIsSign() != null, "is_sign", bo.getIsSign());
        lqw.eq(bo.getSignTime() != null, "sign_time", bo.getSignTime());
        lqw.eq(bo.getIsRegister() != null, "is_register", bo.getIsRegister());
        lqw.eq(StringUtils.isNotBlank(bo.getPhotoPath()), "photo_path", bo.getPhotoPath());
        lqw.eq(StringUtils.isNotBlank(bo.getSex()), "sex", bo.getSex());
        lqw.eq(StringUtils.isNotBlank(bo.getTag()), "tag", bo.getTag());
        lqw.eq(StringUtils.isNotBlank(bo.getUserArea()), "user_area", bo.getUserArea());
        lqw.like(StringUtils.isNotBlank(bo.getUserAreaName()), "user_area_name", bo.getUserAreaName());
        lqw.eq(bo.getAddTime() != null, "add_time", bo.getAddTime());
        lqw.eq(StringUtils.isNotBlank(bo.getAddBy()), "add_by", bo.getAddBy());
        lqw.eq(StringUtils.isNotBlank(bo.getEditer()), "editer", bo.getEditer());
        lqw.eq(bo.getEditTime() != null, "edit_time", bo.getEditTime());
        lqw.eq(StringUtils.isNotBlank(bo.getNation()), "nation", bo.getNation());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), "address", bo.getAddress());
        lqw.eq(bo.getBirthday() != null, "birthday", bo.getBirthday());
        lqw.eq(StringUtils.isNotBlank(bo.getIssue()), "issue", bo.getIssue());
        lqw.eq(bo.getIssueDate() != null, "issueDate", bo.getIssueDate());
        lqw.eq(StringUtils.isNotBlank(bo.getExpiryDate()), "expiryDate", bo.getExpiryDate());
        lqw.eq(StringUtils.isNotBlank(bo.getReferrerBy()), "referrer_by", bo.getReferrerBy());
        lqw.eq(StringUtils.isNotBlank(bo.getAlipayLogonId()), "alipay_logon_id", bo.getAlipayLogonId());
        lqw.eq(bo.getStatus() != null, "status", 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;
    }
}
