package com.mosty.base.manager.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.manager.mapper.VwSydwCyryGxMapper;
import com.mosty.base.manager.mapper.VwSyrkJzgjGxMapper;
import com.mosty.base.manager.service.VwSyrkJzgjGxService;
import com.mosty.base.model.entity.afjcsj.VwSydwCyryGx;
import com.mosty.base.model.entity.afjcsj.VwSyrkJzgjGx;
import com.mosty.base.model.query.afjcsj.VwSyrkJzgjGxListQuery;
import com.mosty.base.model.query.afjcsj.VwSyrkJzgjGxQuery;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@AllArgsConstructor
public class VwSyrkJzgjGxServiceImpl extends ServiceImpl<VwSyrkJzgjGxMapper, VwSyrkJzgjGx>
        implements VwSyrkJzgjGxService {

    private final VwSydwCyryGxMapper vwSydwCyryGxMapper;

    @Override
    public IPage<VwSyrkJzgjGx> selectPageList(VwSyrkJzgjGxQuery dto) {
        Map<String, Object> map = new HashMap<>();
        map.put("dto", dto);
        map.put("pageIndex", (dto.getPageCurrent() - 1) * dto.getPageSize());
        map.put("pageSize", dto.getPageSize());
        int count = this.baseMapper.getCount(map);
        List<VwSyrkJzgjGx> list = this.baseMapper.getList(map);
        IPage<VwSyrkJzgjGx> page = new Page<>(dto.getPageCurrent(), dto.getPageSize());
        page.setTotal(count);
        page.setRecords(list);
        return page;
    }

    @Override
    public IPage<VwSyrkJzgjGx> selectGjList(VwSyrkJzgjGxListQuery dto) {
        return this.baseMapper.selectPage(
                new Page<>(dto.getPageCurrent(), dto.getPageSize()),
                new LambdaQueryWrapper<VwSyrkJzgjGx>()
                        .eq(VwSyrkJzgjGx::getXtZxbz, "0")
                        .eq(VwSyrkJzgjGx::getJzdFxjdm, "510109000000")
                        .eq(VwSyrkJzgjGx::getZjhm, dto.getZjhm())
        );
    }

    @Override
    public List<Map<String, Object>> getStatistics() {
        return this.baseMapper.getStatistics();
    }

    @Override
    public List<VwSydwCyryGx> getSyrkCygj(String zjhm) {
        return this.vwSydwCyryGxMapper.selectList(
                new LambdaQueryWrapper<VwSydwCyryGx>()
                        .eq(VwSydwCyryGx::getXtZxbz, "0")
                        .eq(VwSydwCyryGx::getZjhm, zjhm)
                        .orderByDesc(VwSydwCyryGx::getBgrq)
        );
    }

    @Override
    public VwSydwCyryGx getSyrkInfo(String id) {
        return this.vwSydwCyryGxMapper.selectOne(
                new LambdaQueryWrapper<VwSydwCyryGx>()
                        .eq(VwSydwCyryGx::getId, id)
                        .last(" limit 1")
        );
    }
}
