package com.xhkjedu.sapi.service.assess;

import com.xhkjedu.sapi.mapper.assess.AssessQuotaMapper;
import com.xhkjedu.sapi.mapper.assess.AssessTemplateMapper;
import com.xhkjedu.sapi.mapper.assess.AssessTemplateQuotaMapper;
import com.xhkjedu.sapi.model.assess.TAssessTemplate;
import com.xhkjedu.sapi.model.assess.TAssessTemplateQuota;
import com.xhkjedu.utils.N_Utils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by ywx on 2021/11/10 10:31.
 */
@Service
public class AssessTemplateService {
    @Resource
    private AssessTemplateMapper assessTemplateMapper;
    @Resource
    private AssessTemplateQuotaMapper assessTemplateQuotaMapper;
    @Resource
    private AssessQuotaMapper assessQuotaMapper;

    //添加
    @Transactional
    public void save(TAssessTemplate model) {
        Integer timestamp = N_Utils.getSecondTimestamp();
        model.setCreatetime(timestamp);
        List<Integer> qutoaids = model.getQutoaids();
        model.setQuotanum(qutoaids.size());
        model.setTemplatestate(1);
        assessTemplateMapper.insertUseGeneratedKeys(model);
        Integer templateid = model.getId();
        //保存模版指标管理关系
        List<TAssessTemplateQuota> atqlst = new ArrayList<>();
        for(Integer id : qutoaids){
            TAssessTemplateQuota atq = new TAssessTemplateQuota();
            atq.setTemplateid(templateid);
            atq.setQuotaid(id);
            atq.setCreateid(model.getCreateid());
            atq.setCreatetime(timestamp);

            atqlst.add(atq);
        }
        //批量保存模版指标关联关系
        assessTemplateQuotaMapper.insertList(atqlst);
    }

    //删除
    public Integer deleteById(Integer id) {
        return assessTemplateMapper.updateTemplatestate(10,id);
    }

    //修改
    @Transactional
    public void update(TAssessTemplate model) {
        //修改模版信息，
        Integer timestamp = N_Utils.getSecondTimestamp();
        model.setCreatetime(timestamp);
        List<Integer> qutoaids = model.getQutoaids();
        model.setQuotanum(qutoaids.size());
        assessTemplateMapper.updateByPrimaryKeySelective(model);

        //删除原有模版指标关联关系，重新添加
        assessTemplateMapper.deleteTemplateQuota(model.getTemplateid());
        List<TAssessTemplateQuota> atqlst = new ArrayList<>();
        for(Integer id : qutoaids){
            TAssessTemplateQuota atq = new TAssessTemplateQuota();
            atq.setTemplateid(model.getTemplateid());
            atq.setQuotaid(id);
            atq.setCreateid(model.getCreateid());
            atq.setCreatetime(timestamp);

            atqlst.add(atq);
        }
        //批量保存模版指标关联关系
        assessTemplateQuotaMapper.insertList(atqlst);
    }

    //获取模版关联的指标id集合
    public List<Integer> listQutosByTemplateid(Integer id) {
        return assessTemplateMapper.listQutosByTemplateid(id);
    }

    //获取列表
    public List <Map> listAll(Integer schoolid) {
        return assessTemplateMapper.listTemplates(schoolid);
    }

    //获取模版下树形指标
    public List<Map> listQuotaByTemplateid(Integer templateid){
        return assessQuotaMapper.listQuotaByTemplateid(templateid);
    }
}
