package com.chinaweal.aiccs.aiccs.fraudreg.service.impl;

import com.chinaweal.aiccs.aiccs.abnormal.entity.Gzaiccode;
import com.chinaweal.aiccs.aiccs.fraudreg.entity.TBizFraud;
import com.chinaweal.aiccs.aiccs.fraudreg.mapper.TBizFraudMapper;
import com.chinaweal.aiccs.aiccs.fraudreg.service.ITBizFraudService;
import com.chinaweal.aiccs.org.service.TUsersService;
import com.chinaweal.aicorg.model.AICUser;
import com.chinaweal.youfool.framework.springboot.mybatis.plus.BaseServiceImpl;
import com.chinaweal.youfool.framework.sso.util.SSOUtil;
import org.springframework.stereotype.Service;
import com.chinaweal.youfool.framework.springboot.base.query.BaseQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 涉嫌冒用登记业务表 服务实现类
 * </p>
 *
 * @author chinaweal
 * @since 2021-11-11
 */
@Service
public class TBizFraudServiceImpl extends BaseServiceImpl<TBizFraudMapper, TBizFraud> implements ITBizFraudService {

    @Resource
    private TBizFraudMapper tBizFraudMapper;
    @Resource
    private TUsersService tUsersService;

    @Override
    public TBizFraud saveTBizFraud(TBizFraud tBizFraud) {
        save(tBizFraud);
        return tBizFraud;
    }

    @Override
    public void updateTBizFraud(TBizFraud tBizFraud) {
        updateById(tBizFraud);
    }

    @Override
    public IPage<TBizFraud> listTBizFraud(BaseQuery<TBizFraud> query) {
        TBizFraud entity = query.getEntity(TBizFraud.class);
        Page<TBizFraud> page = query.getPage();

        LambdaQueryWrapper<TBizFraud> lambdaQueryWrapper = Wrappers.lambdaQuery();

        return page(page, lambdaQueryWrapper);
    }

    @Override
    public TBizFraud getTBizFraudByBizSeq(String bizSeq) {
        return this.lambdaQuery().eq(TBizFraud::getBizSeq, bizSeq).one();
    }

    @Override
    public List<Map<String, String>> statistics() {
        // 根据机关查询
        AICUser aicUser = SSOUtil.getUser();
        Gzaiccode gzaiccode = tUsersService.findOrganization(aicUser.getPrimaryKey());

        // 非市局只能看本级
        String aicCode = null;
        if (!"440101".equals(aicUser.getRegionID())) {
            aicCode = gzaiccode.getScountrycode();
        }
        return tBizFraudMapper.statistics(aicCode);
    }


}
