package cn.gov.customs.wxjy.statReport.controller;

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

import cn.gov.customs.cacp.sdks.core.result.Result;
import cn.gov.customs.cacp.sdks.core.user.trans.CacpTransUser;
import cn.gov.customs.cacp.sdks.core.user.trans.UserContextHolder;
import cn.gov.customs.wxjy.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import cn.gov.customs.wxjy.common.core.controller.BaseController;
import org.springframework.web.multipart.MultipartFile;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.gov.customs.wxjy.statReport.pojo.CustomMStatF;
import cn.gov.customs.wxjy.statReport.pojo.CustomMStatFQuery;
import cn.gov.customs.wxjy.statReport.service.ICustomMStatFService;
import cn.gov.customs.wxjy.common.utils.poi.ExcelUtil;
import cn.gov.customs.wxjy.common.core.page.TableDataInfo;

/**
 * 按业务现场关区分类Controller
 *
 * @author xiong
 * @date 2025-12-12
 */
@RestController
@RequestMapping("/statReport/CustomMStatF")
@RequiredArgsConstructor
public class CustomMStatFController extends BaseController {

    private final ICustomMStatFService customMStatFService;

    /**
     * 查询按业务现场关区分类列表
     */
    @PostMapping("/get-list")
    public Result<PageInfo<CustomMStatF>> list(@RequestBody CustomMStatFQuery query) {
//        PageHelper.startPage(query.getPageIndex(), query.getPageSize());
        if(StringUtils.isNotEmpty(query.getBeginDate())){
            query.setBeginDate(query.getBeginDate().replace("-", ""));
        }
        PageInfo<CustomMStatF> list = customMStatFService.selectPageList(query);
        return Result.success(list);
    }

    /**
     * 获取按业务现场关区分类详细信息
     */
    @GetMapping(value = "/get-detail")
    public Result<CustomMStatF> getInfo(@RequestParam String ID) {
        return Result.success(customMStatFService.selectByID(ID));
    }

    /**
     * 新增按业务现场关区分类
     */
    @PostMapping("/insert-customMStatF")
    public Result<Integer> add(@RequestBody CustomMStatF customMStatF) {
        return Result.success(customMStatFService.insert(customMStatF));
    }

    /**
     * 修改按业务现场关区分类
     */
    @PostMapping("/update-customMStatF")
    public Result<Integer> edit(@RequestBody CustomMStatF customMStatF) {
        return Result.success(customMStatFService.update(customMStatF));
    }

    /**
     * 删除按业务现场关区分类
     */
    @PostMapping("/delete-customMStatF")
    public Result<Integer> remove(@RequestBody String[] ids) {
        return Result.success(customMStatFService.deleteByIDs(ids));
    }

    /**
    * 导入 按业务现场关区分类
    * @param file
    * @param updateSupport
    * @return
    * @throws Exception
    */
    @PostMapping("/import-customMStatF")
    public Result<String> importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        CacpTransUser user = UserContextHolder.currentUser();
        ExcelUtil<CustomMStatF> util = new ExcelUtil<CustomMStatF>(CustomMStatF.class);
        List<CustomMStatF> customMStatFList = util.importExcel(file.getInputStream());
        String message = customMStatFService.importCustomMStatF(customMStatFList, updateSupport, user);
        return Result.success(message);
    }

    /**
     * 导出按业务现场关区分类列表
     */
    @PostMapping("/get-customMStatF-export")
    public void export(HttpServletResponse response, @RequestBody CustomMStatFQuery query) throws IOException {
        if(StringUtils.isNotEmpty(query.getBeginDate())){
            query.setBeginDate(query.getBeginDate().replace("-", ""));
        }
        List<CustomMStatF> list = customMStatFService.selectList(query);
        ExcelUtil<CustomMStatF> util = new ExcelUtil<CustomMStatF>(CustomMStatF.class);
        util.exportExcel(response, list, "按业务现场关区分类数据");
    }

    /**
    * 获取导入模板
    * @param response
    */
    @PostMapping("/template-customMStatF")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<CustomMStatF> util = new ExcelUtil<CustomMStatF>(CustomMStatF.class);
        util.importTemplateExcel(response, "按业务现场关区分类");
    }
}
