package org.dromara.payment.worker.service.impl;

import org.dromara.common.core.enums.BusRole;
import org.dromara.common.core.service.BaseService;
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.dromara.common.satoken.utils.LoginHelper;
import org.springframework.stereotype.Service;
import org.dromara.payment.worker.domain.bo.UserWorkerAuthorizedBo;
import org.dromara.payment.worker.domain.vo.UserWorkerAuthorizedVo;
import org.dromara.payment.worker.domain.UserWorkerAuthorized;
import org.dromara.payment.worker.mapper.UserWorkerAuthorizedMapper;
import org.dromara.payment.worker.service.IUserWorkerAuthorizedService;

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

/**
 * 授权协议Service业务层处理
 *
 * @author LionLi
 * @date 2024-04-29
 */
@RequiredArgsConstructor
@Service
public class UserWorkerAuthorizedServiceImpl extends BaseService implements IUserWorkerAuthorizedService {

    private final UserWorkerAuthorizedMapper baseMapper;

    /**
     * 查询授权协议
     */
    @Override
    public UserWorkerAuthorizedVo queryById(Long id){
        if(id == null){
            this.invalidationParamsException("签约ID不能为空");
        }
        UserWorkerAuthorizedBo bo = new UserWorkerAuthorizedBo();
        bo.setId(id);
        return baseMapper.selUserWorkerAuthorInfo(bo);
    }

    /**
     * 查询授权协议列表
     */
    @Override
    public TableDataInfo<UserWorkerAuthorizedVo> queryPageList(UserWorkerAuthorizedBo bo, PageQuery pageQuery) {
        QueryWrapper<UserWorkerAuthorized> lqw = buildQueryWrapper(bo);
        Page<UserWorkerAuthorizedVo> result = baseMapper.selUserWorkerAuthorList(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询授权协议列表
     */
    @Override
    public List<UserWorkerAuthorizedVo> queryList(UserWorkerAuthorizedBo bo) {
        QueryWrapper<UserWorkerAuthorized> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private QueryWrapper<UserWorkerAuthorized> buildQueryWrapper(UserWorkerAuthorizedBo bo) {
        Map<String, Object> params = bo.getParams();
        QueryWrapper<UserWorkerAuthorized> lqw = new QueryWrapper<>();
        lqw.between(bo.getStartTime() != null,"t.create_time",bo.getStartTime(),bo.getEndTime());
        lqw.like(StringUtils.isNotEmpty(bo.getOpName()),"t2.name",bo.getOpName());
        lqw.like(StringUtils.isNotEmpty(bo.getWorkName()),"t1.name",bo.getWorkName());
        lqw.like(StringUtils.isNotEmpty(bo.getIdCard()),"t1.card_id",bo.getIdCard());
        lqw.eq(bo.getSignStatus() != null, "sign_status", bo.getSignStatus());
        Integer busRole = LoginHelper.getBusRole();
        if(busRole == BusRole.OPERATOR.getId()){
            lqw.eq("t.op_id",LoginHelper.getBusId());
        }
        lqw.eq(StringUtils.isNotBlank(bo.getNo()), "no", bo.getNo());
        lqw.orderByDesc("t.create_time");
        return lqw;
    }

    /**
     * 新增授权协议
     */
    @Override
    public Boolean insertByBo(UserWorkerAuthorizedBo bo) {
        UserWorkerAuthorized add = MapstructUtils.convert(bo, UserWorkerAuthorized.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改授权协议
     */
    @Override
    public Boolean updateByBo(UserWorkerAuthorizedBo bo) {
        UserWorkerAuthorized update = MapstructUtils.convert(bo, UserWorkerAuthorized.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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