package com.ssj.service.sys.orgclass;

import com.alibaba.dubbo.config.annotation.Service;
import com.ssj.bean.sys.orgclass.VipBindClass;
import com.ssj.bean.sys.orgclass.VipBindClassTemp;
import com.ssj.bean.weixin.libmy.domain.LibVip;
import com.ssj.bean.weixin.libmy.domain.TbLibManager;
import com.ssj.dao.sys.orgclass.VipBindClassTempDao;
import com.ssj.dao.weixin.library.dao.IBookManagerDao;
import com.ssj.dao.weixin.library.dao.ILibVipDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.framework.core.persistence.DynamicSpecifications;
import com.ssj.framework.core.persistence.SearchFilter;
import com.ssj.framework.core.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(version = "1.0")
@Transactional
public class VipBindClassTempServiceImpl extends BaseServiceImpl<VipBindClassTemp, String> implements VipBindClassTempService {

    @Autowired
    private VipBindClassTempDao vipBindClassTempDao;

    @Autowired
    private VipBindClassService vipBindClassService;

    @Autowired
    private ILibVipDao vipDao;

    @Autowired
    private IBookManagerDao managerDao;

    @Override
    public PagingAndSortingRepository<VipBindClassTemp, String> getDao() {
        return vipBindClassTempDao;
    }

    @Override
    public Page<VipBindClassTemp> findByPage(Map<String, Object> params, Pageable sp) {
        Map<String, SearchFilter> filters = SearchFilter.parse(params);
        Specification<VipBindClassTemp> specification = DynamicSpecifications.bySearchFilter(filters.values(), VipBindClassTemp.class);
        return vipBindClassTempDao.findAll(specification, sp);
    }

    @Override
    public void importCheck(){
        //只校验失败和未校验的
        List<VipBindClassTemp> list = vipBindClassTempDao.findByStatusInOrStatusIsNull(Arrays.asList("CHECK_FAIL", "IMPORT_FAIL"));
        for (VipBindClassTemp temp : list) {
            //1.查找指定的小孩，失败情况：查到多个小孩；找不到指定的小孩
            List<LibVip> vipList = vipDao.findByChildNameAndGradeAndSchoolAndParentName(temp.getChildName(), temp.getGrade(), temp.getSchool(), temp.getParentName());
            if (vipList.size() == 0) {
                temp.setStatus("CHECK_FAIL");
                temp.setErrorReason("没有找到指定小孩");
            } else if (vipList.size() > 1) {
                temp.setStatus("CHECK_FAIL");
                temp.setErrorReason("找到多个小孩");
            } else {
                temp.setStatus("CHECK_SUCCESS");
                temp.setVipId(vipList.get(0).getId());
            }
            if(!"CHECK_FAIL".equals(temp.getStatus())){
                //2.查找指定老师，失败情况：查到多个老师；找不到指定老师
                List<TbLibManager> teacherList = managerDao.findByNameAndTelephoneAndStateAndType(temp.getTeacherName(), temp.getTeacherPhone(), 1, 10);
                if (teacherList.size() == 0) {
                    temp.setStatus("CHECK_FAIL");
                    temp.setErrorReason(temp.getErrorReason() == null ? "没有找到指定老师" : temp.getErrorReason() + "；没有找到指定老师");
                } else if (teacherList.size() > 1) {
                    temp.setStatus("CHECK_FAIL");
                    temp.setErrorReason(temp.getErrorReason() == null ? "找到多个老师" : temp.getErrorReason() + "；找到多个老师");
                } else {
                    temp.setStatus("CHECK_SUCCESS");
                    temp.setErrorReason(null);
                    temp.setTeacherId(teacherList.get(0).getUserId());
                }
            }
        }
        this.save(list);
    }

    /**
     * 确认导入
     */
    @Override
    public void comfirmImport() {
        //只导入校验成功的
        List<VipBindClassTemp> list = vipBindClassTempDao.findByStatusInOrStatusIsNull(Arrays.asList("CHECK_SUCCESS"));
        Date now = new Date();
        for (VipBindClassTemp temp : list) {
            VipBindClass entity = new VipBindClass();
            entity.setCreateTime(now);
            entity.setGrade(temp.getGrade());
            entity.setOrgName(temp.getOrgName());
            entity.setSchoolClass(temp.getSchoolClass());
            entity.setSubject(temp.getSubject());
            entity.setTeacherId(temp.getTeacherId());
            entity.setVipId(temp.getVipId());
            vipBindClassService.save(entity);
            temp.setStatus("IMPORT_SUCCESS");
            this.save(temp);
        }
    }
}
