package com.jojubanking.boot.module.system.service.wqda;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jojubanking.boot.module.system.dal.dataobject.wqda.WqdaDOx;
import com.jojubanking.boot.module.system.dal.dataobject.wqwt.WqwtDO;
import com.jojubanking.boot.module.system.dal.mysql.wq.WqMapper;
import com.jojubanking.boot.module.system.dal.mysql.wqda.WqdaxMapper;
import com.jojubanking.boot.module.system.dal.mysql.wqwt.WqwtMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.text.DecimalFormat;
import java.util.*;
import com.jojubanking.boot.module.system.controller.admin.wqda.vo.*;
import com.jojubanking.boot.module.system.dal.dataobject.wqda.WqdaDO;
import com.jojubanking.boot.framework.common.pojo.PageResult;

import com.jojubanking.boot.module.system.convert.wqda.WqdaConvert;
import com.jojubanking.boot.module.system.dal.mysql.wqda.WqdaMapper;

import static com.jojubanking.boot.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.jojubanking.boot.module.system.enums.ErrorCodeConstants.*;

/**
 * 问卷调查答案 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class WqdaServiceImpl implements WqdaService {

    @Resource
    private WqdaMapper wqdaMapper;
    @Resource
    private WqdaxMapper wqdaxMapper;

    @Resource
    private WqwtMapper wqwtMapper;

    @Override
    public Integer createWqda(WqdaCreateReqVO createReqVO) {
        // 插入
        WqdaDO wqda = WqdaConvert.INSTANCE.convert(createReqVO);
        wqdaMapper.insert(wqda);
        // 返回
        return wqda.getId();
    }

    @Override
    public void updateWqda(WqdaUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateWqdaExists(updateReqVO.getId());
        // 更新
        WqdaDO updateObj = WqdaConvert.INSTANCE.convert(updateReqVO);
        wqdaMapper.updateById(updateObj);
    }

    @Override
    public void deleteWqda(Integer id) {
        // 校验存在
        this.validateWqdaExists(id);
        // 删除
        wqdaMapper.deleteById(id);
    }

    private void validateWqdaExists(Integer id) {
        if (wqdaMapper.selectById(id) == null) {
            throw exception(WQDA_NOT_EXISTS);
        }
    }

    @Override
    public WqdaDO getWqda(Integer id) {
        return wqdaMapper.selectById(id);
    }

    @Override
    public List<WqdaDO> getWqdaList(Collection<Integer> ids) {
        return wqdaMapper.selectBatchIds(ids);
    }

    @Override
    public String getWqdaSumList(String typeId, String startTime, String endTime){
        //住院101，门诊102
        LambdaQueryWrapper<WqwtDO> wtwrapper = new LambdaQueryWrapper<>();
        wtwrapper.ne(WqwtDO::getQuestionType, "03").eq(WqwtDO::getTypeId, typeId).orderByAsc(WqwtDO::getQuestionId);
        List<WqwtDO> list = wqwtMapper.selectList(wtwrapper);

        String all = "[";
        for(int i=0; i<list.size(); i++)
        {
	        String questionId = list.get(i).getQuestionId();

//            LambdaQueryWrapper<WqdaDO> wrapper = new LambdaQueryWrapper<>();
//            wrapper.eq(WqdaDO::getQuestionId, questionId).groupBy(WqdaDO::getQuestionAnswer).orderBy(true,true,WqdaDO::getQuestionId);
//            List<WqdaDO> lsttmp = wqdaMapper.selectList(wrapper);

            List<WqdaDOx> lsttmp = wqdaxMapper.selectSumList(questionId, typeId, startTime, endTime);
            //计算progress
            Long total = Long.valueOf(0);
            for(int j=0; j<lsttmp.size(); j++)
            {
                total = total + lsttmp.get(j).getSum();
            }
            for(int k=0; k<lsttmp.size(); k++)
            {
                //progress 赋值
                double pro1 = (double)lsttmp.get(k).getSum() / (double)total;
                pro1 = pro1 * 100d;
                DecimalFormat df2 = new DecimalFormat("###");
                String pro2 = df2.format(pro1);
                Long pro3 = Long.valueOf(pro2);
                lsttmp.get(k).setProgress(pro3);

                //questionLabel
                WqwtDO wqwtDO = new WqwtDO();
                wqwtDO = wqwtMapper.selectOne("question_id", lsttmp.get(k).getQuestionId());

                lsttmp.get(k).setQuestionLabel(wqwtDO.getQuestionTitle());

//                String stroption = wqwtMapper.getOne(lsttmp.get(k).getQuestionId(), lsttmp.get(k).getQuestionAnswer());
//                lsttmp.get(k).setQuestionTitle(stroption);
            }

//            all = all + "{\"data\":" + lsttmp.size() + "},";
            //每个list当做json的一个array
            if(lsttmp.size() != 0){
//                all = all + "\"" +list.get(i).getQuestionTitle() + "\":" + JSON.toJSONString(lsttmp) + ",";
                all = all + "{\"data\":" + JSON.toJSONString(lsttmp) + ",";
                all = all +
                        "\"props\": {\"stripe\": true},\"columns\": [{ \"label\": \"选项名称\", \"prop\": \"questionTitle\"  }, " +
                        "{ \"label\": \"小计\", \"prop\": \"sum\" }, " +
                        "{ \"label\": \"百分比\", \"prop\": \"progress\" " +
                        "}]},";
            }
        }
        all = all.substring(0, all.length() -1);
        if(all.length() != 0){
            all = all + "]";
        }else {
            all = "[]";
        }

        //汇总所有的array，到前端统计显示

        return all;
    }

    @Override
    public PageResult<WqdaDO> getWqdaPage(WqdaPageReqVO pageReqVO) {
        //按客户要求，需要将其他内容全部放到问题答案中
//        PageResult<WqdaDOx> resPage;
//        List<WqdaDOx> lst = new ArrayList<>();
//
//        PageResult<WqdaDO> Page = wqdaMapper.selectPage(pageReqVO);


        return wqdaMapper.selectPage(pageReqVO);
    }

    @Override
    public List<WqdaDO> getWqdaList(WqdaExportReqVO exportReqVO) {
        return wqdaMapper.selectList(exportReqVO);
    }

}
