package com.mosty.yjzl.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mosty.base.model.entity.yjzl.TbYjmxCsb;
import com.mosty.base.model.query.yjzl.TbYjmxCsbQuery;
import com.mosty.base.model.vo.yjzl.TbYjmxCsbVo;
import com.mosty.yjzl.mapper.TbYjmxCsbMapper;
import com.mosty.yjzl.service.TbYjmxCsbService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class TbYjmxCsbServiceImpl extends ServiceImpl<TbYjmxCsbMapper, TbYjmxCsb>
        implements TbYjmxCsbService {

    @Override
    public int insert(TbYjmxCsbVo csb) {
        TbYjmxCsb vo = new TbYjmxCsb();
        BeanUtils.copyProperties(csb, vo);
        vo.setXtSjly("1");
        return this.baseMapper.insert(vo);
    }



    @Override
    public TbYjmxCsbVo queryById(String id) {
        TbYjmxCsb tbYjmxCsb = baseMapper.selectById(id);
        TbYjmxCsbVo vo = new TbYjmxCsbVo();
        BeanUtils.copyProperties(tbYjmxCsb, vo);
        return vo;
    }

    @Override
    public int deleteById(String id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public IPage<TbYjmxCsbVo> getPage(TbYjmxCsbQuery dto) {
        IPage<TbYjmxCsb> page = this.baseMapper.selectPage(
                new Page<>(dto.getPageCurrent(),dto.getPageSize()),
                new LambdaQueryWrapper<TbYjmxCsb>()
                        .eq(StringUtils.isNotBlank(dto.getMxid()),TbYjmxCsb::getMxid,dto.getMxid())
                        .like(StringUtils.isNotBlank(dto.getCsdm()),TbYjmxCsb::getCsdm,dto.getCsdm())
                        .eq(StringUtils.isNotBlank(dto.getCsmc()),TbYjmxCsb::getCsmc,dto.getCsmc())
                        .eq(StringUtils.isNotBlank(dto.getCsz()),TbYjmxCsb::getCsz,dto.getCsz())
                        .eq(StringUtils.isNotBlank(dto.getCslx()),TbYjmxCsb::getCslx,dto.getCslx())
        );

        IPage<TbYjmxCsbVo> pageVo = new Page<>(dto.getPageCurrent(),dto.getPageSize());
        List<TbYjmxCsbVo> voList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(page.getRecords())){
            page.getRecords().forEach(item->{
                TbYjmxCsbVo vo = new TbYjmxCsbVo();
                BeanUtils.copyProperties(item,vo);
                voList.add(vo);
            });
        }
        pageVo.setRecords(voList);
        pageVo.setTotal(page.getTotal());
        return pageVo;
    }

    @Override
    public int updateById(TbYjmxCsbVo clVo) {
        TbYjmxCsb csb = new TbYjmxCsb();
        BeanUtils.copyProperties(clVo,csb);
        csb.setXtSjly("1");
        return this.baseMapper.updateById(csb);
    }
}
