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

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.manage.WebsitGoodsPriceVO;
import com.gree.mall.manager.bean.material.manage.WebsitPriceUpdBean;
import com.gree.mall.manager.bean.material.manage.WebsitPriceUpdVO;
import com.gree.mall.manager.enums.material.MaterialFlagEnum;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.material.manage.WebsitGoodsPriceLogic;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

@Slf4j
@RestController
@Api(value = "网点价格管理API", tags ={"网点价格管理API"} )
@Validated
@RequestMapping(value = "/websit/goods/price", produces = "application/json; charset=utf-8")
public class WebsitGoodsPriceController {

    @Resource
    WebsitGoodsPriceLogic websitGoodsPriceLogic;

    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "网点价格管理-列表")
    public ResponseHelper<IPage<WebsitGoodsPriceVO>> page(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<WebsitGoodsPriceVO> page = websitGoodsPriceLogic.page(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<WebsitGoodsPriceVO>() {});
    }

    @PostMapping("/list/export")
    @ApiOperation("网点价格管理-导出")
    public void listExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //2.查询要导出的内容
        IPage<WebsitGoodsPriceVO> baseVOIPage = websitGoodsPriceLogic.page(zfireParamBean);
        //3.导出
        FieldUtils.exportData(baseVOIPage.getRecords(), zfireParamBean.getExportFields(), request, response);
    }

    @ZfireList
    @PostMapping("upd/list")
    @ApiOperation(value = "网点调价管理-列表")
    public ResponseHelper<IPage<WebsitPriceUpdVO>> updPage(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<WebsitPriceUpdVO> page = websitGoodsPriceLogic.updPage(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<WebsitPriceUpdVO>() {});
    }

    @PostMapping("upd/list/export")
    @ApiOperation("网点调价管理-导出")
    public void updListExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //2.查询要导出的内容
        IPage<WebsitPriceUpdVO> baseVOIPage = websitGoodsPriceLogic.updPage(zfireParamBean);
        //3.导出
        FieldUtils.exportData(baseVOIPage.getRecords(), zfireParamBean.getExportFields(), request, response);
    }

    @PostMapping("/detail")
    @ApiOperation(value = "网点调价管理-详情")
    public ResponseHelper<WebsitPriceUpdBean> detail(
            @ApiParam(value = "sheetId", required = true) @RequestParam String sheetId
    ) throws RemoteServiceException {
        WebsitPriceUpdBean bean = websitGoodsPriceLogic.detail(sheetId);
        return ResponseHelper.success(bean);
    }

    @PostMapping("/add")
    @ApiOperation(value = "网点调价管理-添加")
    public ResponseHelper add(
            @Valid @RequestBody WebsitPriceUpdBean bean
    ) throws Exception {
        websitGoodsPriceLogic.add(bean);
        return ResponseHelper.success();
    }

    @PostMapping("/edit")
    @ApiOperation(value = "网点调价管理-编辑")
    public ResponseHelper edit(
            @Valid @RequestBody WebsitPriceUpdBean bean
    ) throws Exception {
        websitGoodsPriceLogic.edit(bean);
        return ResponseHelper.success();
    }

    @PostMapping("/confirm")
    @ApiOperation(value = "网点调价管理-审核")
    public ResponseHelper confirm(
            @ApiParam(value = "单号", required = true) @NotBlank(message = "单号不能为空") @RequestParam String sheetId,
            @ApiParam(value = "审核结果 OK=通过 FAIL=失败", required = true) @NotNull(message = "审核结果不能为空") @RequestParam MaterialFlagEnum flag
    ) throws Exception {
        websitGoodsPriceLogic.confirm(sheetId, flag);
        return ResponseHelper.success();
    }
}
