package common;

import com.BWebApplication;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.hai.common.Base64Util;
import com.hai.common.security.AESEncodeUtil;
import com.hai.entity.HighDiscountAgentCode;
import com.hai.entity.SecRegion;
import com.hai.service.CommonService;

import com.hai.service.HighDiscountAgentCodeService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import javax.annotation.Resource;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName RegionTest
 * @Description: TODO ()
 * @Author 胡锐
 * @Date 2020/12/29
 **/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = BWebApplication.class)
@WebAppConfiguration
public class RegionTest {

    @Resource
    private CommonService commonService;

    @Resource
    private HighDiscountAgentCodeService highDiscountAgentCodeService;

    @Test
    public void addLogs(){
        try {
            OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("exampleWrite.json"),"UTF-8");
            List<Map<String,Object>> jobTypeList = new ArrayList<>();
            List<Map<String,Object>> children1;

            List<SecRegion> parentRegion = commonService.getCities();
            for (SecRegion parent : parentRegion) {
                Map<String,Object> map = new HashMap<>();
                map.put("value", parent.getRegionId());
                map.put("label", parent.getRegionName());

                // 查询二级
                List<SecRegion> chinRegion = commonService.getRegionsByParentId(parent.getRegionId());
                children1 = new ArrayList<>();
                for (SecRegion chin : chinRegion) {
                    Map<String,Object> map1 = new HashMap<>();
                    map1.put("value", chin.getRegionId());
                    map1.put("label", chin.getRegionName());
                    children1.add(map1);
                }

                map.put("children", children1);
                jobTypeList.add(map);
            }

            osw.write(JSON.toJSONString(jobTypeList));
            osw.flush();//清空缓冲区，强制输出数据
            osw.close();//关闭输出流
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Test
    public void simpleWrite() throws Exception {
        // 写法1
        String fileName = "D:\\simpleWrite.xlsx";


        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("discountAgentId", "");
        List<HighDiscountAgentCode> codeList = highDiscountAgentCodeService.getDiscountCode(paramMap);

        List<ExcelModel> list = new ArrayList<>();
        ExcelModel excelModel;

        Map<String,Object> map = new HashMap<>();
        map.put("type", "DISCOUNT");

        for (HighDiscountAgentCode code : codeList) {
            excelModel = new ExcelModel();
            map.put("id", code.getId());
            String param = "https://hsg.dctpay.com/wx/?action=gogogo&id=" + Base64Util.encode(AESEncodeUtil.aesEncrypt(JSON.toJSONString(map)));
            excelModel.setCodeUrl(param);
            list.add(excelModel);
        }
        // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
        // 如果这里想使用03 则 传入excelType参数即可
        EasyExcel.write(fileName, ExcelModel.class).sheet("模板").doWrite(list);
    }
}
