package org.dromara.system.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.system.domain.CountData;
import org.dromara.system.domain.bo.CountDataBo;
import org.dromara.system.domain.vo.CountDataVo;
import org.dromara.system.mapper.TblCountDataMapper;
import org.dromara.system.service.ITblCountDataService;
import org.springframework.stereotype.Service;

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

/**
 * 丢失货物Service业务层处理
 *
 * @author Lion Li
 * @date 2024-02-22
 */
@RequiredArgsConstructor
@DS("slave")
@Slf4j
@Service
public class TblCountDataServiceImpl implements ITblCountDataService {

    private final TblCountDataMapper baseMapper;

    /**
     * 查询丢失货物
     */
    @Override
    public CountDataVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    @Override
    public CountDataVo queryByDate(Map<String, Object> bo) {
        return baseMapper.selectdatabydate(bo);
    }

    /**
     * 查询丢失货物列表
     */
    @Override
    public TableDataInfo<CountDataVo> queryPageList(CountDataBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CountData> lqw = buildQueryWrapper(bo);
        Page<CountDataVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
//        log.info(JSONUtil.toJsonStr(result.getRecords()));
        return TableDataInfo.build(result);
    }

    /**
     * 查询丢失货物列表
     */
    @Override
    public List<CountDataVo> queryList(CountDataBo bo) {
        LambdaQueryWrapper<CountData> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CountData> buildQueryWrapper(CountDataBo bo) {
        LambdaQueryWrapper<CountData> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDevicecode()!=null,CountData::getDevicecode, bo.getDevicecode());
        if(bo.getParams()!=null && bo.getParams().keySet().size()>0) {
            if (bo.getParams().get("starttime") != null && bo.getParams().get("endtime") != null) {
                lqw.gt(bo.getParams().get("starttime") != null, CountData::getTs, bo.getParams().get("starttime"));
                lqw.lt(bo.getParams().get("endtime") != null, CountData::getTs, bo.getParams().get("endtime"));
            }
            if(bo.getParams().get("deviceCodes")!=null){
                if(bo.getParams().get("deviceCodes") instanceof List){
                    lqw.in(CountData::getDevicecode, String.join(",",(List) bo.getParams().get("deviceCodes")));
                }else{
                    lqw.eq(CountData::getDevicecode, bo.getParams().get("deviceCodes"));
                }
            }
        }

        return lqw;
    }

    /**
     * 新增丢失货物
     */
    @Override
    public Boolean insertByBo(CountData bo) {
//        CountData add = MapstructUtils.convert(bo, CountData.class);
        validEntityBeforeSave(bo);
        boolean flag = baseMapper.insert(bo) > 0;
        if (flag) {

        }
        return flag;
    }

    @Override
    public Boolean insertData(CountData bo) {
        baseMapper.insertdata(bo);
        return true;
    }

    /**
     * 修改丢失货物
     */
    @Override
    public Boolean updateByBo(CountData bo) {
        CountData update = MapstructUtils.convert(bo, CountData.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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