package org.dromara.payment.thirdPlatform.xinyujian.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
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.payment.thirdPlatform.xinyujian.domain.ThirdItemTaskTypes;
import org.dromara.payment.thirdPlatform.xinyujian.domain.bo.ThirdItemTaskTypesBo;
import org.dromara.payment.thirdPlatform.xinyujian.domain.vo.ThirdItemTaskTypesVo;
import org.dromara.payment.thirdPlatform.xinyujian.mapper.ThirdItemTaskTypesMapper;
import org.springframework.stereotype.Service;

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

/**
 * 三方平台企业的项目任务类型Service业务层处理
 *
 * @author LionLi
 * @date 2025-03-30
 */
@RequiredArgsConstructor
@Service
public class ThirdItemTaskTypesServiceImpl implements IThirdItemTaskTypesService {

    private final ThirdItemTaskTypesMapper baseMapper;

    /**
     * 查询三方平台企业的项目任务类型
     */
    @Override
    public ThirdItemTaskTypesVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询三方平台企业的项目任务类型列表
     */
    @Override
    public TableDataInfo<ThirdItemTaskTypesVo> queryPageList(ThirdItemTaskTypesBo bo, PageQuery pageQuery) {
        QueryWrapper<ThirdItemTaskTypes> lqw = buildQueryWrapper(bo);
        Page<ThirdItemTaskTypesVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询三方平台企业的项目任务类型列表
     */
    @Override
    public List<ThirdItemTaskTypesVo> queryList(ThirdItemTaskTypesBo bo) {
        QueryWrapper<ThirdItemTaskTypes> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private QueryWrapper<ThirdItemTaskTypes> buildQueryWrapper(ThirdItemTaskTypesBo bo) {
        Map<String, Object> params = bo.getParams();
        QueryWrapper<ThirdItemTaskTypes> lqw = new QueryWrapper<>();
        lqw.eq(StringUtils.isNotBlank(bo.getBusinessId()), "business_id", bo.getBusinessId());
        lqw.eq(bo.getParkId() != null, "park_id", bo.getParkId());
        lqw.eq(StringUtils.isNotBlank(bo.getItemType()), "item_type", bo.getItemType());
        lqw.like(StringUtils.isNotBlank(bo.getItemTypeName()), "item_type_name", bo.getItemTypeName());
        lqw.eq(StringUtils.isNotBlank(bo.getTaskType()), "task_type", bo.getTaskType());
        lqw.like(StringUtils.isNotBlank(bo.getTaskTypeName()), "task_type_name", bo.getTaskTypeName());
        lqw.like(StringUtils.isNotBlank(bo.getInvoiceOneName()), "invoice_one_name", bo.getInvoiceOneName());
        lqw.like(StringUtils.isNotBlank(bo.getInvoiceTwoName()), "invoice_two_name", bo.getInvoiceTwoName());
        lqw.eq(StringUtils.isNotBlank(bo.getServiceType()), "service_type", bo.getServiceType());
        lqw.eq(StringUtils.isNotBlank(bo.getDeliveries()), "deliveries", bo.getDeliveries());
        lqw.eq(bo.getIsUse() != null, "is_use", bo.getIsUse());
        return lqw;
    }

    /**
     * 新增三方平台企业的项目任务类型
     */
    @Override
    public Boolean insertByBo(ThirdItemTaskTypesBo bo) {
        ThirdItemTaskTypes add = MapstructUtils.convert(bo, ThirdItemTaskTypes.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改三方平台企业的项目任务类型
     */
    @Override
    public Boolean updateByBo(ThirdItemTaskTypesBo bo) {
        ThirdItemTaskTypes update = MapstructUtils.convert(bo, ThirdItemTaskTypes.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 批量删除三方平台企业的项目任务类型
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
