package com.hc.web.controller.browser.back;

import com.alibaba.excel.EasyExcel;
import com.hc.business.dto.OrdersAllUserDto;
import com.hc.business.service.OrderUserLogService;
import com.hc.core.utils.result.Result;
import com.hc.web.config.ExcelWidthStyleStrategy;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;


/**
 * (OrderUserLog)表控制层
 *
 * @author java
 * @since 2020-12-03 09:25:12
 */

@RestController
@RequestMapping("/new/api/back/orderUserLog")
public class BackOrderUserLogController {

    /**
     * 服务对象
     */
    private final OrderUserLogService orderUserLogService;

    @Value("${setting.downloadPath}")
    public String downloadPath;
    public BackOrderUserLogController(OrderUserLogService orderUserLogService) {
        this.orderUserLogService = orderUserLogService;
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "依据主键获取 OrderUserLog", response = Result.class)
    public Result selectBackOrderUserLog(@PathVariable("id") Integer id) {
        return Result.success(this.orderUserLogService.queryById(id));
    }

    @GetMapping("/order/{orderId}")
    @ApiOperation(value = "依据订单id 获取 OrderUserLog", response = Result.class)
    public Result selectOrderUserLogByOrderId(@ApiParam(value = "订单id") @PathVariable("orderId") Integer orderId) {
        return Result.success(orderUserLogService.queryListByOrderId(orderId));
    }


    @GetMapping("/queryUserLogByOrderId")
    @ApiOperation(value = "获取订单下的学员列表", response = Result.class)
    public Result queryUserLogByOrderId(@ApiParam(value = "订单ID") @RequestParam(required = false) Integer orderId) {
        return Result.success(orderUserLogService.queryUserLogByOrderId(orderId));
    }

    @GetMapping("/queryAllOrdersUserExport")
    @ApiOperation(value = "订单-学员全部导出", response = Result.class)
    public Result queryAllOrdersUserExport(@ApiParam(value = "订单号") @RequestParam(required = false) String orderNo,
                                           @ApiParam(value = "订单id") @RequestParam(required = false) Integer orderId,
                                           @ApiParam(value = "班次id") @RequestParam(required = false) Integer classId,
                                           @ApiParam(value = "0未支付 1，成功 2退费 3部分退费 null查全部") @RequestParam(required = false) Integer status,
                                           @ApiParam(value = "支付类型0123，0支付宝，1微信，2微信移动端，3线下汇款 null查全部") @RequestParam(required = false) Integer payType,
                                           @ApiParam(value = "是否付款过 0否 1 是 null 全部") @RequestParam(required = false) Integer isPay,
                                           @ApiParam(value = "创建时间开始毫秒时间戳，默认3个月") @RequestParam(required = false) Long begin,
                                           @ApiParam(value = "创建时间结束毫秒时间戳，默认3个月") @RequestParam(required = false) Long end) {

        // 上传的文件将保存在项目运行目录下的 uploadFile 文件夹
        String realPath = downloadPath;

        // 并且在 uploadFile 文件夹中通过日期对上传的文件归类保存
        // 比如：/uploadFile/2019/06/06/32091e5f-c9e9-4506-9567-43e724f1fe37.png
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/");
        String format = sdf.format(new Date());
        File folder = new File(realPath + format);
        if (!folder.isDirectory()) {
            folder.mkdirs();
        }

        List<OrdersAllUserDto> dataList = this.orderUserLogService.queryAllOrdersUser(orderId, orderNo, status, payType,isPay, classId, begin, end);
        // 对上传的文件重命名，避免文件重名
        String newName = format + UUID.randomUUID() + "订单-学员导出.xlsx";
        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(realPath + newName);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        List<String> strList = Arrays.asList("orderStatus", "type","payTime");
        EasyExcel.write(outputStream, OrdersAllUserDto.class).excludeColumnFieldNames(strList).registerWriteHandler(new ExcelWidthStyleStrategy()).sheet("订单-学员导出").doWrite(dataList);
        return Result.success("/jxjyfile/" + newName);
    }
}