package com.xhkjedu.sapi.service.scantron;

import com.alibaba.fastjson.JSON;
import com.xhkjedu.sapi.config.ConfigKey;
import com.xhkjedu.sapi.mapper.scantron.ScantronMapper;
import com.xhkjedu.sapi.model.scantron.TScantron;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.vo.ResultVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * @Description：答题卡
 * @Author: WN
 * @Date: 2024/10/25 9:52:16
 **/
@Service
public class ScantronService {
    @Resource
    private ScantronMapper scantronMapper;

    /*
     * @Description 答题卡列表
     * @Date 2024/10/25 10:15:03
     * @Author WN
     * @Param [tScantron]
     * @Return java.util.List<com.xhkjedu.sapi.model.scantron.TScantron>
     **/
    public List<Map<String,Object>> listScantrons(TScantron tScantron){
        return scantronMapper.listScantrons(tScantron);
    }

    //答题卡-保存
    @Transactional(rollbackFor = Exception.class)
    public String save(TScantron tScantron) throws Exception{
        tScantron.setCreatetime(N_Utils.getSecondTimestamp());
        if(tScantron.getSlayout() > 1){
            tScantron.setPapertype(1);
        }else{
            tScantron.setPapertype(2);
        }
        String base64Path = tScantron.getPdfpath();
        tScantron.setPdfpath(null);
        scantronMapper.insertUseGeneratedKeys(tScantron);

        Integer sid = tScantron.getId();
        String pdfpath = saveScantronPdf(base64Path,tScantron.getSname(),sid,tScantron.getSlayout(),tScantron.getSchoolid());
        if(N_Utils.isNotEmpty(pdfpath)){
            scantronMapper.updatePdfpath(sid,pdfpath);//更改转为路径的pdf地址
        }
        return pdfpath;
    }

    //答题卡-保存pdf
    private String saveScantronPdf(String pdfpath,String sname,Integer sid,Integer column,Integer schoolid) throws Exception {
        String cloudcode = scantronMapper.getSchoolCloud();

        String pdfbase64 = URLEncoder.encode(pdfpath,"UTF-8");
        String savefolder = cloudcode + "/" + schoolid + "/scantron/";
        String params = "base64path=" + pdfbase64 + "&savefolder=" + savefolder + "&column=" + column + "&filename=" + sname + "&customefolder=" + sid;
        String fileInfoStr = N_Utils.sendPost(ConfigKey.fileapi + "bs/topdf", params);
        if(N_Utils.isEmpty(fileInfoStr)){
            return "";
        }
        ResultVo fileResult = JSON.parseObject(fileInfoStr, ResultVo.class);
        if (fileResult.getCode() == 0 && N_Utils.isNotEmpty(fileResult.getObj())) {
            return fileResult.getObj().toString();
        }else{
            return "";
        }
    }

    //答题卡-修改
    public String update(TScantron tScantron) throws Exception{
        if(N_Utils.isTrueInteger(tScantron.getSlayout()) && tScantron.getSlayout() > 1){
            tScantron.setPapertype(1);
        }else{
            tScantron.setPapertype(2);
        }

        TScantron oldScantron = scantronMapper.selectByPrimaryKey(tScantron.getSid());
        String rtnPdfpath = oldScantron.getPdfpath();
        if(N_Utils.isEmpty(oldScantron.getPdfpath()) || !oldScantron.getPdfpath().equals(tScantron.getPdfpath())){
            String base64Path = tScantron.getPdfpath();
            String sname = tScantron.getSname();
            if(N_Utils.isEmpty(sname)){
                sname = oldScantron.getSname();
            }
            String pdfpath = saveScantronPdf(base64Path,sname,tScantron.getSid(),tScantron.getSlayout(),oldScantron.getSchoolid());
            tScantron.setPdfpath(pdfpath);
            rtnPdfpath = pdfpath;
        }

        scantronMapper.updateByPrimaryKeySelective(tScantron);
        return rtnPdfpath;
    }

    //答题卡-删除
    public Integer delete(Integer sid){
        return scantronMapper.deleteByPrimaryKey(sid);
    }

    //答题卡-详情
    public TScantron detail(Integer sid){
        return scantronMapper.selectByPrimaryKey(sid);
    }

    //答题卡-修改公开状态
    public void updateScantronPubliched(Integer sid, Integer published){
        scantronMapper.updateScantronPubliched(sid,published);
    }

    //答题卡-复用
    public TScantron copyScantron(TScantron scantron){
        //获取答题卡信息
        TScantron originScantron = scantronMapper.selectByPrimaryKey(scantron.getSid());
        originScantron.setSid(null);
        originScantron.setCreateid(scantron.getCreateid());
        originScantron.setCreatetime(N_Utils.getSecondTimestamp());
        originScantron.setPubliched(2);
        scantronMapper.insertUseGeneratedKeys(originScantron);
        originScantron.setSid(originScantron.getId());
        return originScantron;
    }

}
