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

import com.chinaweal.aiccs.aiccs.abnormal.entity.Gzaiccode;
import com.chinaweal.aiccs.aiccs.abnormal.entity.Toincludeabn;
import com.chinaweal.aiccs.aiccs.abnormal.service.TSBizSpeListService;
import com.chinaweal.aiccs.aiccs.abnormal.service.ToincludeabnService;
import com.chinaweal.aiccs.aiccs.market.entity.TSSueInspect;
import com.chinaweal.aiccs.aiccs.market.mapper.TSSueInspectMapper;
import com.chinaweal.aiccs.aiccs.market.service.ITSSueInspectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chinaweal.aiccs.common.util.StringUtils;
import com.chinaweal.aiccs.org.service.TUsersService;
import com.chinaweal.aicorg.model.AICUser;
import com.chinaweal.youfool.framework.springboot.exception.custom.BusinessException;
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.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 应诉检查表  服务实现类
 * </p>
 *
 * @author chinaweal
 * @since 2021-10-13
 */
@Service
public class TSSueInspectServiceImpl extends BaseServiceImpl<TSSueInspectMapper, TSSueInspect> implements ITSSueInspectService {

    @Resource
    private ToincludeabnService toincludeabnService;
    @Resource
    private TUsersService tUsersService;
    @Resource
    private TSBizSpeListService tsBizSpeListService;

    @Override
    public TSSueInspect saveTSSueInspect(TSSueInspect tssueinspect) {
        AICUser user = SSOUtil.getUser();
        Gzaiccode gzaiccode = tUsersService.findOrganization(user.getPrimaryKey());
        tssueinspect.setInspectOrg(gzaiccode.getScountrycode());
        tssueinspect.setStatus("0");
        save(tssueinspect);
        return tssueinspect;
    }

    @Override
    public void updateTSSueInspect(TSSueInspect model) {

        boolean isToIn = true; // 是否需要拟列入
        TSSueInspect tSUsualInspectDB = this.getById(model.getId());
        String status = tSUsualInspectDB.getStatus();

        if ("1".equals(status)) {
            throw new BusinessException("检查状态异常，保存失败");
        }

        // 列异回退处理
        if ("2".equals(status)) {
            // 结果相同重新推送，不相同则废弃
            if (tSUsualInspectDB.getInspectResult().equals(model.getInspectResult())) {
                tsBizSpeListService.updateClue("update", model.getId());
                isToIn = false;
            } else {
                tsBizSpeListService.updateClue("delete", model.getId());
            }
        }


        // 判断是否满足列异 2-未公示 3-弄虚作假 4-实地查无 6-情节严重
        if ("2346".contains(model.getInspectResult()) && isToIn) {
            tsBizSpeListService.clueAdd(model.getPriPId(), model.getInspectResult(), model.getInspectorId(), model.getInspector(), "4", model.getId());
        }

        model.setStatus("1");
        updateById(model);
    }

    @Override
    public boolean backCheck(String id, String opinion) {
        TSSueInspect tsSueInspect = this.getById(id);
        tsSueInspect.setStatus("2");
        tsSueInspect.setBackOpinion(opinion);
        this.updateById(tsSueInspect);
        return true;
    }

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

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

        return page(page, lambdaQueryWrapper);
    }

    @Override
    public List<TSSueInspect> getListByPripid(String pripid) {
        LambdaQueryWrapper<TSSueInspect> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TSSueInspect::getPriPId,pripid).eq(TSSueInspect::getStatus,"1");
        return list(wrapper);
    }
}
