
package com.tmerclub.cloud.flow.controller.platform;

import com.tmerclub.cloud.api.order.vo.FormExcelParamVO;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.i18n.I18nMessage;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.util.BeanUtil;
import com.tmerclub.cloud.common.util.ExcelUtil;
import com.tmerclub.cloud.flow.dto.FormDTO;
import com.tmerclub.cloud.flow.model.Form;
import com.tmerclub.cloud.flow.service.FormService;
import com.tmerclub.cloud.flow.vo.FormExcelVO;
import com.tmerclub.cloud.flow.vo.FormItemVO;
import com.tmerclub.cloud.flow.vo.FormVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 数据表格
 *
 * @author TRACK
 */
@RestController("platformFormController")
@RequestMapping("/p/form")
@Tag(name = "数据表格")
public class FormController {

    @Autowired
    private FormService formService;


    @GetMapping("/page")
    @Operation(summary = "获取数据表格列表", description = "分页获取数据表格列表")
    public ServerResponseEntity<PageVO<Form>> page(@Valid PageDTO pageDTO, FormDTO formParam) {
        formParam.setShopId(Constant.PLATFORM_SHOP_ID);
        PageVO<Form> formPage = formService.page(pageDTO, formParam);
        return ServerResponseEntity.success(formPage);
    }

    @GetMapping
    @Operation(summary = "获取数据表格", description = "根据formId获取数据表格")
    public ServerResponseEntity<Form> getByFormId(@RequestParam Long formId) {
        Form form = formService.getByFormId(formId);
        if (Objects.isNull(form)) {
            throw new LuckException("当前报表不存在，请刷新页面重试");
        }
        return ServerResponseEntity.success(form);
    }

    @PostMapping
    @Operation(summary = "保存数据表格", description = "保存数据表格")
    public ServerResponseEntity<Void> save(@Valid @RequestBody FormDTO formDTO) {
        Form form = BeanUtil.map(formDTO, Form.class);
        form.setFormId(null);
        if (!formDTO.getRecommendForm()) {
            form.setShopId(Constant.PLATFORM_SHOP_ID);
        }
        formService.save(form);
        //如果是推荐报表，则清除缓存
        if (formDTO.getRecommendForm()) {
            formService.removeCache();
        }
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新数据表格", description = "更新数据表格")
    public ServerResponseEntity<Void> update(@Valid @RequestBody FormDTO formDTO) {
        if (!formDTO.getRecommendForm()) {
            Form formDb = formService.getByFormId(formDTO.getFormId());
            if (!Objects.equals(formDb.getShopId(), Constant.PLATFORM_SHOP_ID)) {
                throw new LuckException("该报表数据有误，请刷新后重新输入");
            }
        }
        Form form = BeanUtil.map(formDTO, Form.class);
        formService.update(form);
        //如果是推荐报表，则清除缓存
        if (formDTO.getRecommendForm()) {
            formService.removeCache();
        }
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除数据表格", description = "根据数据表格id删除数据表格")
    public ServerResponseEntity<Void> delete(@RequestParam Long formId) {
        Form form = formService.getByFormId(formId);
        formService.deleteById(formId);
        //如果是推荐报表，则清除缓存
        if (Objects.isNull(form.getShopId())) {
            formService.removeCache();
        }
        return ServerResponseEntity.success();
    }

    @GetMapping("/getFormItem")
    @Operation(summary = "获取报表项列表", description = "获取报表项列表")
    @Parameter(name = "type", description = "1:平台端 2：商家端")
    public ServerResponseEntity<List<FormItemVO>> getFormItem(@RequestParam("type") Integer type) {
        List<FormItemVO> formItemEnumList = formService.getFormItem(type, I18nMessage.getLang());
        return ServerResponseEntity.success(formItemEnumList);
    }

    @GetMapping("/formExcel")
    @Operation(summary = "生成报表", description = "生成报表")
    public void formExcel(@RequestParam("formId") Long formId, HttpServletResponse response) {
        Form form = formService.getByFormId(formId);
        if (Objects.isNull(form)) {
            throw new LuckException("报表不存在");
        }
        FormExcelParamVO excelParam = formService.getExcelParam(form);
        List<FormExcelVO> list = formService.listExcel(form, excelParam);
        ExcelUtil.soleExcel(response, list, form.getFormName(), FormExcelVO.MERGE_ROW_INDEX, FormExcelVO.MERGE_COLUMN_INDEX, excelParam.getFormItemNames(), FormExcelVO.class);
    }

    @GetMapping("/getRecommendFormPage")
    @Operation(summary = "分页获取推荐报表", description = "分页获取推荐报表")
    public ServerResponseEntity<PageVO<Form>> getRecommendFormPage(FormDTO form, PageDTO page) {
        PageVO<Form> formPage = formService.getRecommendFormPage(page, form);
        return ServerResponseEntity.success(formPage);
    }

    @GetMapping("/getRecommendFormList")
    @Operation(summary = "获取推荐报表", description = "获取推荐报表")
    public ServerResponseEntity<List<FormVO>> getRecommendFormList() {
        List<FormVO> formList = formService.getRecommendFormList();
        return ServerResponseEntity.success(formList);
    }
}
