package cn.com.countrygarden.bi.main.controller.propertyAgreement;

import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.dto.propertyAgreement.PropertyAgreementDto;
import cn.com.countrygarden.bi.main.service.propertyAgreement.PropertyAgreementService;
import cn.com.countrygarden.bi.main.util.OSSUtils;
import cn.com.countrygarden.bi.main.util.wcx.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.concurrent.TimeUnit;

/**
 * 
 * @author ferren
 * @date 2020/1/10 14:07
 * @param
 * @return 
 */
@RestController
@RequestMapping("/api/service/propertyAgreement")
public class PropertyAgreementController {
    @Autowired
    private PropertyAgreementService propertyAgreementService;

    @Autowired
    private OSSUtils ossUtils;

    @PostMapping("/pageList")
    public Result selectPageList(PropertyAgreementDto param){
        return Response.response(propertyAgreementService.selectPageList(param));
    }

    @PostMapping("organ/list")
    public Result selectOrganList(){
        return Response.response(propertyAgreementService.selectOrganList());
    }

    @PostMapping("/synchronizeData")
    public Result synchronizeData(@RequestParam(value = "startTime", required = false) String startTime,
                                   @RequestParam(value = "nowTime", required = false) String nowTime) throws IOException {
        return Response.response(propertyAgreementService.synchronizeData());
    }

    @PostMapping("/synchronizeDataTest")
    public Result synchronizeDataTest(@RequestParam(value = "areaid", required = false) String areaid,
            @RequestParam(value = "startTime", required = false) String startTime,
                                   @RequestParam(value = "nowTime", required = false) String nowTime) throws IOException {
        return Response.response(propertyAgreementService.synchronizeDataTest(startTime,nowTime,areaid));
    }

    @RequestMapping("preview")
    public void preview( HttpServletResponse response,String url) throws IOException {
        InputStream is = new URL(url).openStream();
        String uuid = CommonUtil.getUUID();
        String s = ossUtils.uploadFileExpired(uuid + ".pdf", is, TimeUnit.MILLISECONDS.toMillis(24 * 3600 * 1000));
        response.sendRedirect(s);
    }
}
