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.VwSydwJbxxGxMapper;
import com.mosty.base.manager.remote.TbBaseAdaptRemoteService;
import com.mosty.base.manager.service.VwSydwJbxxGxService;
import com.mosty.base.model.entity.afjcsj.VwSydwCyryGx;
import com.mosty.base.model.entity.afjcsj.VwSyrkJzgjGx;
import com.mosty.base.model.query.afjcsj.VwSydwCyryPageQuery;
import com.mosty.base.model.query.afjcsj.VwSydwJbxxGxQuery;
import com.mosty.base.model.entity.afjcsj.VwSydwJbxxGx;
import com.mosty.base.model.vo.base.DeptInfoVo;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

/**
 * @author dw
 * @since 2022-10-27
 */
@AllArgsConstructor
@Service
public class VwSydwJbxxGxServiceImpl extends ServiceImpl<VwSydwJbxxGxMapper, VwSydwJbxxGx> implements VwSydwJbxxGxService {

    private final TbBaseAdaptRemoteService tbBaseAdaptRemoteService;
    private final VwSydwCyryGxMapper vwSydwCyryGxMapper;

    @Override
    public IPage<VwSydwJbxxGx> selectList(VwSydwJbxxGxQuery 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.selectSydwCount(map);
        List<VwSydwJbxxGx> list = this.baseMapper.selectSydwList(map);
        IPage<VwSydwJbxxGx> page = new Page<>(dto.getPageCurrent(), dto.getPageSize());
        list.forEach(item -> {
            String sszrqdm = item.getSszrqdm();
            if (StringUtils.isNotBlank(sszrqdm) && sszrqdm.length() >= 12) {
                DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByOrgcode(sszrqdm.substring(0, 12));
                if (dept != null) {
                    item.setSszrqmc(dept.getDeptname());
                }
            }
        });
        page.setTotal(count);
        page.setRecords(list);
        return page;
    }

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

    @Override
    public VwSydwJbxxGx getSydwInfo(String id) {
        VwSydwJbxxGx gx = this.baseMapper.selectOne(
                new LambdaQueryWrapper<VwSydwJbxxGx>()
                        .eq(VwSydwJbxxGx::getId, id)
                        .last("limit 1")
        );
        String sszrqdm = gx.getSszrqdm();
        if (StringUtils.isNotBlank(sszrqdm) && sszrqdm.length() >= 12) {
            DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByOrgcode(sszrqdm.substring(0, 12));
            if (dept != null) {
                gx.setSszrqmc(dept.getDeptname());
            }
        }
        return gx;
    }

    @Override
    public IPage<VwSydwCyryGx> getSydwCyryPage(VwSydwCyryPageQuery dto) {
        return this.vwSydwCyryGxMapper.selectPage(
                new Page<>(dto.getPageCurrent(), dto.getPageSize()),
                new LambdaQueryWrapper<VwSydwCyryGx>()
                        .eq(VwSydwCyryGx::getXtZxbz, "0")
                        .eq(VwSydwCyryGx::getDwid, dto.getDwid())
        );
    }
}
