package com.ym.mec.web.controller;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.entity.FinancialExpenditure;
import com.ym.mec.biz.dal.entity.Goods;
import com.ym.mec.biz.dal.entity.StudentPaymentRouteOrder;
import com.ym.mec.biz.dal.enums.TemplateTypeEnum;
import com.ym.mec.biz.service.FinancialExpenditureService;
import com.ym.mec.biz.service.GoodsService;
import com.ym.mec.biz.service.StudentPaymentRouteOrderService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.util.date.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;

@RequestMapping("import")
@Api(tags = "数据导入服务")
@RestController
public class ImportController extends BaseController {

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private FinancialExpenditureService financialExpenditureService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private StudentPaymentRouteOrderService studentPaymentRouteOrderService;

    @ApiOperation(value = "导入商品")
    @PostMapping(value = "goods")
    @PreAuthorize("@pcs.hasPermissions('import/goods')")
    public HttpResponseResult<List<Goods>> importGoods(@RequestParam("file") MultipartFile file) throws Exception {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        return succeed(goodsService.importGoods(file, sysUser.getId()));
    }

    @ApiOperation(value = "导入财务支出")
    @PostMapping(value = "financialExpenditure")
    @PreAuthorize("@pcs.hasPermissions('import/financialExpenditure')")
    public HttpResponseResult<List<FinancialExpenditure>> importFinancialExpenditure(@RequestParam("file") MultipartFile file) throws Exception {
        return succeed(financialExpenditureService.importFinancialExpenditure(file));
    }

    @ApiOperation(value = "导入财务管理订单")
    @PostMapping(value = "routeOrder")
    @PreAuthorize("@pcs.hasPermissions('import/routeOrder')")
    public HttpResponseResult<List<StudentPaymentRouteOrder>> importRouteOrder(@RequestParam("file") MultipartFile file) throws Exception {
        return studentPaymentRouteOrderService.importRouteOrder(file);
    }

    @ApiOperation(value = "下载导入模板")
    @GetMapping(value = "downloadTemplate")
    @PreAuthorize("@pcs.hasPermissions('import/downloadTemplate')")
    public void getGoodsTemplate(HttpServletResponse response, TemplateTypeEnum templateType) throws IOException {
        InputStream inputStream = new ClassPathResource("excelTemplate/" + templateType.getMsg() + ".xls").getInputStream();
        OutputStream outputStream = response.getOutputStream();
        try {
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition", "attachment;filename=" + templateType.getMsg() + "-" + DateUtil.getDate(new Date()) + ".xls");
            response.flushBuffer();
            outputStream = response.getOutputStream();
            IOUtils.copy(inputStream, outputStream);
            outputStream.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
