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

import com.chinaweal.aiccs.aiccs.abnormal.entity.EBaseinfo;
import com.chinaweal.aiccs.aiccs.abnormal.service.EBaseinfoService;
import com.chinaweal.aiccs.aiccs.fraudreg.entity.TFraudCheck;
import com.chinaweal.aiccs.aiccs.fraudreg.mapper.TFraudCheckMapper;
import com.chinaweal.aiccs.aiccs.fraudreg.service.ITFraudCheckService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chinaweal.aiccs.aiccs.market.entity.TSUsualInspect;
import com.chinaweal.aiccs.aiccs.market.service.ITSUsualInspectService;
import com.chinaweal.aiccs.common.util.StringUtils;
import com.chinaweal.aiccs.org.service.TUsersService;
import com.chinaweal.aicorg.model.AICUser;
import com.chinaweal.aicorg.services.OrgUM;
import com.chinaweal.youfool.framework.springboot.mybatis.plus.BaseServiceImpl;
import com.chinaweal.youfool.framework.sso.util.SSOUtil;
import org.springframework.context.annotation.Lazy;
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.Optional;

/**
 * <p>
 * 撤销登记案件转现场核查表 服务实现类
 * </p>
 *
 * @author chinaweal
 * @since 2022-01-13
 */
@Service
public class TFraudCheckServiceImpl extends BaseServiceImpl<TFraudCheckMapper, TFraudCheck> implements ITFraudCheckService {

    @Resource
    @Lazy
    private ITSUsualInspectService iTSUsualInspectService;
    @Resource
    private OrgUM orgUM;
    @Resource
    private EBaseinfoService eBaseinfoService;
    @Resource
    @Lazy
    private ITSUsualInspectService itsUsualInspectService;

    @Override
    public TFraudCheck saveTFraudCheck(TFraudCheck tFraudCheck) {
        save(tFraudCheck);
        return tFraudCheck;
    }

    @Override
    public void updateTFraudCheck(TFraudCheck tFraudCheck) {

        if ("2".equals(tFraudCheck.getStatus())) { // 分派人员, 生成动态监管
            AICUser acceptUser = orgUM.findUserByPrimaryKey(tFraudCheck.getSendUserId());
            EBaseinfo eBaseInfo = eBaseinfoService.getOneByPripid(tFraudCheck.getPripid());

            TSUsualInspect tsUsualInspect = new TSUsualInspect();
            tsUsualInspect.setPriPId(eBaseInfo.getPripid());
            tsUsualInspect.setRegNo(eBaseInfo.getRegno());
            tsUsualInspect.setUscc(eBaseInfo.getUniscid());
            tsUsualInspect.setEntName(eBaseInfo.getEntname());
            tsUsualInspect.setLegalName(eBaseInfo.getName());
            tsUsualInspect.setDom(StringUtils.isNotBlank(eBaseInfo.getDom()) ? eBaseInfo.getDom() : eBaseInfo.getAddress());
            tsUsualInspect.setContacts(eBaseInfo.getName());
            tsUsualInspect.setContactPhone(eBaseInfo.getPhone());
            tsUsualInspect.setMobilePhone(eBaseInfo.getPhone());
            tsUsualInspect.setScope(eBaseInfo.getOpscope());
            tsUsualInspect.setInspectType("撤销案件登记现场核查");
            tsUsualInspect.setInspectorId(acceptUser.getPrimaryKey());
            tsUsualInspect.setInspector(acceptUser.getName());
            tsUsualInspect.setInspectOrg(acceptUser.getRegionID());
            tsUsualInspect.setInspectOrgName(acceptUser.getOrgName());
            tsUsualInspect.setStatus("0");
            itsUsualInspectService.saveTSUsualInspect(tsUsualInspect);

            tFraudCheck.setInspectId(tsUsualInspect.getId());
        }
        updateById(tFraudCheck);
    }

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

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

        return page(page, lambdaQueryWrapper);
    }

    @Override
    public void pushCheckStatus(String inspectId) {
        Optional<TFraudCheck> oneOpt = this.lambdaQuery().eq(TFraudCheck::getInspectId, inspectId).oneOpt();

        if (oneOpt.isPresent()) {
            TFraudCheck tFraudCheck = oneOpt.get();
            tFraudCheck.setStatus("3");
            this.updateById(tFraudCheck);
        }
    }

    @Override
    public void batchUpdateTFraudCheck(List<TFraudCheck> tFraudCheckList) {
        for (TFraudCheck tFraudCheck : tFraudCheckList) {
            updateTFraudCheck(tFraudCheck);
        }
    }

}
