package com.galaxis.manatee.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.galaxis.manatee.entity.chuanyun.*;
import com.galaxis.manatee.entity.chuanyun.bo.MaterialResendBO;
import com.galaxis.manatee.entity.chuanyun.data.object.ProjectDO;
import com.galaxis.manatee.entity.chuanyun.data.object.ProjectLogDO;
import com.galaxis.manatee.manager.ChuanYunManager;
import org.hibernate.Session;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.EntityManager;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zcj
 * @version 0.1
 * @date 2020/2/5 4:03 下午
 */
@RestController
public class ChuanyunTestController {

    private ChuanYunManager chuanYunManager;
    private EntityManager entityManager;
//    public ChuanyunTestController(ChuanYunManager chuanYunManager) {
//        this.chuanYunManager = chuanYunManager;
//    }


    public ChuanyunTestController(ChuanYunManager chuanYunManager, EntityManager entityManager) {
        this.chuanYunManager = chuanYunManager;
        this.entityManager = entityManager;
    }

    /**
     * 测试
     */
    @GetMapping("/testEntity")
    public void test() throws JsonProcessingException {
        Session session=entityManager.unwrap(Session.class);
        List<Object> testList=session.createSQLQuery("select * from baba").list();
        ObjectMapper objectMapper=new ObjectMapper();
        System.out.println(objectMapper.writeValueAsString(testList));
    }

    /**
     * 单条查询测试
     * @return  测试结果
     */
    @GetMapping("/chuanyunTest/testFind")
    public String testFind() throws JsonProcessingException {
        ObjectMapper objectMapper=new ObjectMapper();
        ChuanyunFindResponse<ProjectLogDO> logTemplateChuanYunFindResponse =chuanYunManager.find(ProjectLogDO.SCHEMA_CODE,"ae5f74e9-a8f8-4d20-9e94-b594e57c12bc");
        return objectMapper.writeValueAsString(logTemplateChuanYunFindResponse);
    }

    /**
     * 单条查询测试
     * @return  测试结果
     */
    @GetMapping("/chuanyunTest/testFindList")
    public String testFindList() throws JsonProcessingException {
        ObjectMapper objectMapper=new ObjectMapper();
        Filter testFilter=Filter.instance(0,20,true);
        ChuanyunFindAllResponse<ProjectDO> logTemplateChuanYunFindAllResponse =chuanYunManager.findAll(ProjectDO.SCHEMA_CODE,testFilter);
        return objectMapper.writeValueAsString(logTemplateChuanYunFindAllResponse);
    }

    /**
     * 保存测试
     * @return  保存结果
     */
    @GetMapping("/chuanyunTest/testSave")
    public String testSave() throws JsonProcessingException {
        MaterialResendBO resendVO=new MaterialResendBO();
        resendVO.setRequestDate(LocalDate.now());
        resendVO.setRequestType("test");
        ObjectMapper objectMapper=new ObjectMapper();
        ChuanyunSaveResponse chuanyunSaveResponse= chuanYunManager.save(MaterialResendBO.SCHEMA_CODE,objectMapper.writeValueAsString(resendVO),true);
        return objectMapper.writeValueAsString(chuanyunSaveResponse);
    }

    /**
     * 保存所有
     * @return  保存结果
     * @throws JsonProcessingException  json处理异常
     */
    @GetMapping("/chuanyunTest/testSaveAll")
    public String testSaveAll() throws JsonProcessingException {
        MaterialResendBO resend1=new MaterialResendBO();
        MaterialResendBO resend2=new MaterialResendBO();
        resend1.setRequestType("helloChuanyunSaveAll1");
        resend2.setRequestType("helloChuanyunSaveAll2");
        ObjectMapper objectMapper=new ObjectMapper();
        List<String> strings=new ArrayList<>();
        strings.add(objectMapper.writeValueAsString(resend1));
        strings.add(objectMapper.writeValueAsString(resend2));
        ChuanyunSaveAllResponse chuanyunSaveAllResponse= chuanYunManager.saveAll(MaterialResendBO.SCHEMA_CODE,strings,true);
        return objectMapper.writeValueAsString(chuanyunSaveAllResponse);
    }
}
