package com.nantian.cloud.jljc.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.jljc.entity.TExperts;
import com.nantian.cloud.jljc.mapper.TExpertsMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nantian.cloud.jljc.service.TExpertsService;
import com.nantian.sc.cloud.common.core.util.R;
import org.apache.ibatis.annotations.Case;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 节能专家库专家库 服务实现类
 * @since 2020-09-05
 */
@Service("tExpertsService")
public class TExpertsServiceImpl extends ServiceImpl<TExpertsMapper, TExperts> implements TExpertsService {

    @Override
    public IPage getExpertsByPage(Page page, TExperts tExperts) {
        IPage expertsByPage = baseMapper.getExpertsByPage(page, tExperts);
        List<TExperts> records = expertsByPage.getRecords();
        List<TExperts> newRecords = new ArrayList<>();
        for (TExperts record : records) {
            newRecords.add(getNameByCode(record));
        }
        expertsByPage.setRecords(newRecords);
        return expertsByPage;
    }

    public List<TExperts> getExperts(String type, String industryCode){
        return baseMapper.getExperts( type,  industryCode);
    }

    @Override
    public TExperts getExpertsById(Integer id) {
        return getNameByCode(baseMapper.getExpertsById(id));
    }

    @Override
    public boolean addExperts(TExperts tExperts) {
        return baseMapper.addExperts(tExperts);
    }

    @Override
    public boolean updateExpertsById(TExperts tExperts) {
        return baseMapper.updateExpertsById(tExperts);
    }

    @Override
    public boolean deleteExpertsById(TExperts tExperts) {
        return baseMapper.deleteExpertsById(tExperts);
    }

    @Override
    public TExperts getNameByCode(TExperts tExperts) {
        if(tExperts.getType()!=null){
            switch (tExperts.getType()){
                case "J":
                    tExperts.setType("节能减排");
                    break;
                case "L":
                    tExperts.setType("计量");
                    break;
                default: //G
                    tExperts.setType("公共机构");
                    break;
            }
        }
        if(tExperts.getIdtype()!=null){
            switch (tExperts.getIdtype()){
                case "1":
                    tExperts.setIdtype("身份证");
                    break;
                case "2":
                    tExperts.setIdtype("驾驶证");
                    break;
                case "3":
                    tExperts.setIdtype("行驶证");
                    break;
                case "4":
                    tExperts.setIdtype("社会保障卡");
                    break;
                case "5":
                    tExperts.setIdtype("护照");
                    break;
                case "6":
                    tExperts.setIdtype("港澳通行证");
                    break;
                default:
                    tExperts.setIdtype("其他证件");
                    break;
            }
        }
        return tExperts;
    }

    @Override
    public R getRandomExperts(TExperts tExperts, Integer num) {
        if(num == null || "".equals(num)){
            return R.failed("num不能为空！");
        }
        Integer len = 0;
        if (tExperts.getPosition() != null && !"".equals(tExperts.getPosition())) {
            String[] split = tExperts.getPosition().split(",");
            len = split.length;
            return R.ok(baseMapper.getRandomExperts(split,num,len.toString()));
        }
        /*
        if(len>4){
            return R.failed("查询内容格式错误！");
        }*/
        //查询内容，支持 职务职称,专业/特长,标签 这种每个条件单选的格式
        return R.ok(baseMapper.getRandomExperts(null,num,len.toString()));
    }
}
