package com.cpop.oam.business.controller.backstage;

import com.alibaba.excel.EasyExcel;
import com.cpop.core.base.R;
import com.cpop.oam.business.bo.DataImportBo;
import com.cpop.oam.business.bo.DataImportPageBo;
import com.cpop.oam.business.dto.DataImportDto;
import com.cpop.oam.business.service.DataImportService;
import com.cpop.oam.business.vo.DataImportPageVo;
import com.cpop.oam.business.vo.StoreListVo;
import com.cpop.system.business.service.BrandService;
import com.cpop.system.business.service.StoreService;
import com.cpop.system.business.vo.BrandListVo;
import com.cpop.system.business.vo.SysFileVo;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import static com.cpop.system.business.entity.table.BrandTableDef.BRAND;
import static com.cpop.system.business.entity.table.StoreTableDef.STORE;

/**
 * oam-数据导入表 控制层。
 *
 * @author DB
 * @since 2023-12-10
 */
@RestController
@Api(tags = "数据导入接口")
@RequestMapping("/backstage/dataImport")
public class BackstageDataImportController {

    @Autowired
    private DataImportService dataImportService;

    @Autowired
    private BrandService brandService;

    @Autowired
    private StoreService storeService;

    /**
     * 数据导入分页查询
     * @author DB
     * @since 2023/12/10
     * @param bo 请求参数
     * @return R<Page<DataImportPageVo>>
     */
    @GetMapping("/getDataImportPage")
    @ApiOperation("数据导入分页查询")
    public R<Page<DataImportPageVo>> getDataImportPage(DataImportPageBo bo) {
        Page<DataImportPageVo> page = dataImportService.getDataImportPage(bo);
        return R.ok(page);
    }

    /**
     * 获取品牌列表
     *
     * @param brandName 品牌名
     * @author DB
     * @since 2023-11-30 17:59:29
     */
    @ApiOperation("获取品牌列表")
    @GetMapping("/getBrandList")
    public R<List<BrandListVo>> getBrandList(@ApiParam("品牌名") String brandName) {
        List<BrandListVo> list = brandService.queryChain().and(BRAND.BRAND_NAME.like(brandName)).listAs(BrandListVo.class);
        return R.ok(list);
    }

    /**
     * 获取校区/店铺列表
     *
     * @param brandId 品牌id
     * @author DB
     * @since 2023-11-30 18:00:08
     */
    @ApiOperation("获取校区/店铺列表")
    @GetMapping("/getStoreList")
    public R<List<StoreListVo>> getStoreList(@ApiParam("品牌id") String brandId) {
        List<StoreListVo> list = storeService.queryChain().and(STORE.BRAND_ID.eq(brandId)).listAs(StoreListVo.class);
        return R.ok(list);
    }

    /**
     * 新增数据导入
     * @author DB
     * @since 2023/12/10
     * @param bo 请求参数
     * @return boolean
     */
    @PostMapping("/insertDataImport")
    @ApiOperation("新增数据导入")
    public R<Void> insertDataImport(@RequestBody @Validated DataImportBo bo) {
        dataImportService.insertDataImport(bo);
        return R.ok();
    }

    /**
     * 根据主键删除oam-数据导入表。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("/removeById/{id}")
    @ApiOperation("根据主键删除未导入数据记录")
    public R<Void> removeById(@PathVariable @ApiParam("数据导入主键") String id) {
        dataImportService.removeById(id);
        return R.ok();
    }

    /**
     * 检查上传的文件
     * @author DB
     * @since 2023/12/10
     * @param file 文件
     * @return R<SysFileVo>
     */
    @ApiOperation("检查上传的文件")
    @PostMapping("/checkImportData")
    public R<SysFileVo> checkImportData(@RequestParam("file") MultipartFile file){
        SysFileVo sysFileVo = dataImportService.checkImportData(file);
        return R.ok(sysFileVo);
    }

    /**
     * 立即导入
     * @author DB
     * @since 2023/12/10
     * @return R<Void>
     */
    @ApiOperation("立即导入")
    @PutMapping("/importNow/{id}")
    public R<Void> importNow(@PathVariable @ApiParam("数据导入主键") String id) {
        dataImportService.importNow(id);
        return R.ok();
    }

    /**
     * 模板下载
     * @author DB
     * @since 2023/12/10
     * @param response 响应
     */
    @ApiOperation("模板下载")
    @GetMapping("/download")
    public void download(HttpServletResponse response) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyExcel没有关系
        String fileName = URLEncoder.encode("数据导入模板", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), DataImportDto.class).sheet("模板").doWrite(new ArrayList<>());
    }

}
