package com.template.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.template.mapper.SmartEvaluateTdetailMapper;
import com.template.mapper.SmartScoreManageMapper;
import com.template.model.evaluate.teacher.*;
import com.template.model.result.PageUtils;
import com.template.services.SmartScoreManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 平分项管理 服务实现类
 * </p>
 *
 * @author ceshi
 * @since 2024-01-02
 */
@Service
public class SmartScoreManageServiceImpl extends ServiceImpl<SmartScoreManageMapper, SmartScoreManage> implements SmartScoreManageService {

    @Autowired
    private SmartScoreManageMapper smartScoreManageMapper;
    @Autowired
    public SmartEvaluateTdetailMapper smartEvaluateTdetailMapper;

    @Override
    public int insertSmartScoreManage(SmartScoreManage sa) {
        int result = smartScoreManageMapper.insert(sa);
        return result;
    }

    @Override
    public int updateSmartScoreManage(SmartScoreManage sa) {
        int result = smartScoreManageMapper.updateById(sa);
        return result;
    }

    @Override
    public PageUtils<SmartScoreManage> queryPageSmartScoreManage(int currentPage, int pageCount, String name) {
        Page<SmartScoreManage> page = new Page<>(currentPage, pageCount);
        QueryWrapper<SmartScoreManage> queryWrapper = new QueryWrapper<>();
        //queryWrapper.like(StringUtils.hasText(name), "name", name);
        IPage<SmartScoreManage> result = smartScoreManageMapper.selectPage(page,queryWrapper);
        return new PageUtils<>(result);
    }

    @Override
    public int deleteSmartScoreManageById(int id) {
        int result = smartScoreManageMapper.deleteById(id);
        return result;
    }

    @Override
    public SmartScoreManage getSmartScoreManageById(int id) {
        SmartScoreManage result = smartScoreManageMapper.selectById(id);
        return result;
    }

    @Override
    public List<SmartScoreManage> getScoreManageList() {
        QueryWrapper<SmartScoreManage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0);
        List<SmartScoreManage> result = smartScoreManageMapper.selectList(queryWrapper);
        return result;
    }

    @Override
    public List<SmartScoreManage> getScoreManageByKey(QueryWrapper<SmartScoreManage> queryWrapper) {
        List<SmartScoreManage> result = smartScoreManageMapper.selectList(queryWrapper);
        return result;
    }

    @Override
    public String getMaxTerm(){
        return smartScoreManageMapper.getMaxTerm();
    }

    @Override
    public SimpleScore queryScoreDetail(String term){
        SimpleScore simpleScore = new SimpleScore();
        QueryWrapper<SmartScoreManage> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("deleted", 0);
        queryWrapper1.eq("parent_id", 0);
        queryWrapper1.eq("term", term);
        List<SmartScoreManage> mainScore = smartScoreManageMapper.selectList(queryWrapper1);
        List<SmartScoreManageList> ls = new ArrayList<>();
        for (int i = 0; i < mainScore.size(); i++) {
            SmartScoreManageList mainScoreManageList = new SmartScoreManageList();
            mainScoreManageList.setId(mainScore.get(i).getId());
            mainScoreManageList.setTerm(mainScore.get(i).getTerm());
            mainScoreManageList.setParentId(mainScore.get(i).getParentId());
            mainScoreManageList.setScoreItem(mainScore.get(i).getScoreItem());
            mainScoreManageList.setScoreNum(mainScore.get(i).getScoreNum());
            mainScoreManageList.setDeleted(mainScore.get(i).getDeleted());
            QueryWrapper<SmartScoreManage> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("deleted", 0);
            queryWrapper2.eq("parent_id", mainScore.get(i).getId());
            List<SmartScoreManage> childScore = smartScoreManageMapper.selectList(queryWrapper2);
            List<SmartScoreManageList> csList = new ArrayList<>();
            for (int j = 0; j < childScore.size(); j++) {
                SmartScoreManageList cs = new SmartScoreManageList();
                cs.setId(childScore.get(j).getId());
                cs.setTerm(childScore.get(j).getTerm());
                cs.setParentId(childScore.get(j).getParentId());
                cs.setScoreItem(childScore.get(j).getScoreItem());
                cs.setScoreNum(childScore.get(j).getScoreNum());
                cs.setDeleted(childScore.get(j).getDeleted());
                csList.add(cs);
            }
            mainScoreManageList.setSmartScoreManageList(csList);
            ls.add(mainScoreManageList);
        }
        simpleScore.setSimpleScoreList(ls);
        return simpleScore;
    }



    @Override
    public SimpleScoreDetailList queryTeacherScoreDetail(String term, String cardNo){
        SimpleScoreDetailList simpleScore = new SimpleScoreDetailList();
        QueryWrapper<SmartEvaluateTdetail> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("deleted", 0);
        queryWrapper1.eq("parent_id", 0);
        queryWrapper1.eq("term", term);
        queryWrapper1.eq("card_no", cardNo);
        List<SmartEvaluateTdetail> mainScore = smartEvaluateTdetailMapper.selectList(queryWrapper1);
        List<SmartEvaluateTdetailList> ls = new ArrayList<>();
        for (int i = 0; i < mainScore.size(); i++) {
            SmartEvaluateTdetailList smartEvaluateTdetail = new SmartEvaluateTdetailList();
            smartEvaluateTdetail.setId(mainScore.get(i).getId());
            smartEvaluateTdetail.setCardNo(mainScore.get(i).getCardNo());
            smartEvaluateTdetail.setTerm(mainScore.get(i).getTerm());
            smartEvaluateTdetail.setParentId(mainScore.get(i).getParentId());
            smartEvaluateTdetail.setScoreItem(mainScore.get(i).getScoreItem());
            smartEvaluateTdetail.setScoreNum(mainScore.get(i).getScoreNum());
            smartEvaluateTdetail.setOwnNum(mainScore.get(i).getOwnNum());
            System.out.println("===="+mainScore.get(i).getOwnNum());
            smartEvaluateTdetail.setDeleted(mainScore.get(i).getDeleted());
            QueryWrapper<SmartEvaluateTdetail> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("deleted", 0);
            queryWrapper2.eq("parent_id", mainScore.get(i).getId());
            List<SmartEvaluateTdetail> childScore = smartEvaluateTdetailMapper.selectList(queryWrapper2);
            List<SmartEvaluateTdetailList> csList = new ArrayList<>();
            for (int j = 0; j < childScore.size(); j++) {
                SmartEvaluateTdetailList cs = new SmartEvaluateTdetailList();
                cs.setId(childScore.get(j).getId());
                cs.setCardNo(childScore.get(j).getCardNo());
                cs.setTerm(childScore.get(j).getTerm());
                cs.setParentId(childScore.get(j).getParentId());
                cs.setScoreItem(childScore.get(j).getScoreItem());
                cs.setScoreNum(childScore.get(j).getScoreNum());
                cs.setOwnNum(childScore.get(j).getOwnNum());
                cs.setDeleted(childScore.get(j).getDeleted());
                csList.add(cs);
            }
            smartEvaluateTdetail.setEvaluateList(csList);
            ls.add(smartEvaluateTdetail);
        }
        simpleScore.setSimpleScoreDetailList(ls);
        return simpleScore;
    }

    @Override
    public SimpleScoreDetailList queryNoTeacherScoreDetail(String term){
        SimpleScoreDetailList simpleScore = new SimpleScoreDetailList();
        QueryWrapper<SmartScoreManage> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("deleted", 0);
        queryWrapper1.eq("parent_id", 0);
        queryWrapper1.eq("term", term);
        List<SmartScoreManage> mainScore = smartScoreManageMapper.selectList(queryWrapper1);
        List<SmartEvaluateTdetailList> ls = new ArrayList<>();
        for (int i = 0; i < mainScore.size(); i++) {
            SmartEvaluateTdetailList mainScoreManageList = new SmartEvaluateTdetailList();
            mainScoreManageList.setId(null);
            mainScoreManageList.setTerm(mainScore.get(i).getTerm());
            mainScoreManageList.setParentId(mainScore.get(i).getParentId());
            mainScoreManageList.setScoreItem(mainScore.get(i).getScoreItem());
            mainScoreManageList.setScoreNum(mainScore.get(i).getScoreNum());
            mainScoreManageList.setOwnNum(0.0);
            mainScoreManageList.setDeleted(mainScore.get(i).getDeleted());
            QueryWrapper<SmartScoreManage> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("deleted", 0);
            queryWrapper2.eq("parent_id", mainScore.get(i).getId());
            List<SmartScoreManage> childScore = smartScoreManageMapper.selectList(queryWrapper2);
            List<SmartEvaluateTdetailList> csList = new ArrayList<>();
            for (int j = 0; j < childScore.size(); j++) {
                SmartEvaluateTdetailList cs = new SmartEvaluateTdetailList();
                cs.setId(null);
                cs.setTerm(childScore.get(j).getTerm());
                cs.setParentId(null);
                cs.setScoreItem(childScore.get(j).getScoreItem());
                cs.setScoreNum(childScore.get(j).getScoreNum());
                cs.setOwnNum(0.0);
                cs.setDeleted(childScore.get(j).getDeleted());
                csList.add(cs);
            }
            mainScoreManageList.setEvaluateList(csList);
            ls.add(mainScoreManageList);
        }
        simpleScore.setSimpleScoreDetailList(ls);
        return simpleScore;
    }
}
