package com.gree.mall.manager.controller.material.base;

import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gree.mall.manager.annotation.ZfireList;
import com.gree.mall.manager.bean.material.base.WebsitNormChargeBean;
import com.gree.mall.manager.bean.material.base.WebsitNormChargeVO;
import com.gree.mall.manager.enums.material.StateEnum;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.material.base.NormChargeLogic;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Slf4j
@RestController
@Api(value = "辅材收费标准API", tags ={"辅材收费标准API"} )
@RequestMapping(value = "/material/norm", produces = "application/json; charset=utf-8")
public class NormChargeController {

    @Resource
    NormChargeLogic normChargeLogic;

    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "辅材收费标准-列表")
    public ResponseHelper<IPage<WebsitNormChargeVO>> page(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<WebsitNormChargeVO> page = normChargeLogic.page(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<WebsitNormChargeVO>() {});
    }

    @PostMapping("/list/export")
    @ApiOperation("辅材收费标准-导出")
    public void listExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //2.查询要导出的内容
        IPage<WebsitNormChargeVO> baseVOIPage = normChargeLogic.page(zfireParamBean);
        //3.导出
        FieldUtils.exportData(baseVOIPage.getRecords(), zfireParamBean.getExportFields(), request, response);
    }

    @PostMapping("/detail")
    @ApiOperation(value = "辅材收费标准-详情")
    public ResponseHelper<WebsitNormChargeBean> detail(
            @ApiParam(value = "id", required = true) @RequestParam String id
    ) throws RemoteServiceException {
        return ResponseHelper.success(normChargeLogic.detail(id));
    }

    @PostMapping("/add")
    @ApiOperation(value = "辅材收费标准-添加")
    public ResponseHelper add(
            @RequestBody WebsitNormChargeBean bean
    ) throws Exception {
        normChargeLogic.add(bean);
        return ResponseHelper.success();
    }

    @PostMapping("/edit")
    @ApiOperation(value = "辅材收费标准-编辑")
    public ResponseHelper edit(
            @RequestBody WebsitNormChargeBean bean
    ) throws Exception {
        normChargeLogic.edit(bean);
        return ResponseHelper.success();
    }

    @PostMapping("/batch/update/status")
    @ApiOperation(value = "辅材收费标准-批量更新状态")
    public ResponseHelper batchUpdateStatus(
            @ApiParam(value = "id", required = true) @RequestParam List<String> ids,
            @ApiParam(value = "ON=有效 OFF=无效", required = true) @RequestParam StateEnum stateEnum
    ) throws Exception {
        normChargeLogic.batchUpdateStatus(ids, stateEnum);
        return ResponseHelper.success();
    }

    @PostMapping("/import")
    @ApiOperation(value = "辅材收费标准-导入(下载模板:辅材收费标准模板（物料收费）.xlsx)")
    public ResponseHelper<String> importData (
            @RequestPart("file") MultipartFile file
    ) throws Exception {
        return ResponseHelper.success(normChargeLogic.importData(file));
    }

    @PostMapping("/import2")
    @ApiOperation(value = "辅材收费标准-导入(下载模板:辅材收费标准模板（服务收费）.xlsx)")
    public ResponseHelper<String> importData2 (
            @RequestPart("file") MultipartFile file
    ) throws Exception {
        return ResponseHelper.success(normChargeLogic.importData2(file));
    }

}
