package com.zfire.mall.manager.controller.manage.product.price;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zfire.mall.manager.bean.ExcelData;
import com.zfire.mall.manager.bean.manage.product.price.ProductUpdPriceBillBean;
import com.zfire.mall.manager.bean.manage.product.price.ProductUpdPriceBillItemBean;
import com.zfire.mall.manager.bean.manage.product.price.ProductUpdPriceBillListBean;
import com.zfire.mall.manager.bean.valid.EditGroup;
import com.zfire.mall.manager.helper.ResponseHelper;
import com.zfire.mall.manager.logic.manage.product.price.ProductUpdPriceBillLogic;
import com.zfire.mall.manager.utils.CommonUtils;
import com.zfire.mall.manager.utils.excel.ExcelUtils;
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 org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.groups.Default;
import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
@Api(value = "产品调价单管理", tags ={"产品调价单管理"} )
@RequestMapping(value = "/product-upd-price", produces = "application/json; charset=utf-8")
public class ProductUpdPriceBillController {

    @Resource
    ProductUpdPriceBillLogic productUpdPriceBillLogic;

    @ApiOperation(value = "产品调价单列表")
    @GetMapping("/list")
    public ResponseHelper<IPage<ProductUpdPriceBillListBean>> list(
            @ApiParam(value = "金蝶编码") @RequestParam(required = false) String materialOldNumber,
            @ApiParam(value = "物料编码") @RequestParam(required = false) String materialNumber,
            @ApiParam(value = "物料名称") @RequestParam(required = false) String materialName,
            @ApiParam(value = "备注") @RequestParam(required = false) String remark,
            @ApiParam(value = "品类id") @RequestParam(required = false) String mainId,
            @ApiParam(value = "销售类型",required = false) @RequestParam(required = false) String saleTypeId,
            @ApiParam(value = "生效日期") @RequestParam(required = false) String startDate,
            @ApiParam(value = "规格",required = false) @RequestParam(required = false) String specification,
            @ApiParam(value = "单据类型 GENERAL=普通 HOME=家用工程 DIFF=收差",required = false) @RequestParam(required = false, defaultValue = "GENERAL") String billType,
            @ApiParam(value = "物料分类编号",required = false) @RequestParam(required = false) String k3CategoryNumber,
            @ApiParam(value = "页号",required = true)@RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true)@RequestParam Integer pageSize
    ) throws Exception {
        IPage<ProductUpdPriceBillListBean> listPage = productUpdPriceBillLogic.listPage(materialOldNumber,materialNumber,
                materialName,remark,mainId,saleTypeId, startDate, specification, billType,k3CategoryNumber, pageNum, pageSize);
        return ResponseHelper.success(listPage);
    }

    @ApiOperation(value = "产品调价单详情")
    @GetMapping("/detail")
    public ResponseHelper<ProductUpdPriceBillBean> detail(
            @ApiParam(value = "产品调价单id", required = true) @RequestParam String id
    ) throws Exception {
        ProductUpdPriceBillBean productUpdPriceBillBean = productUpdPriceBillLogic.detail(id);
        return ResponseHelper.success(productUpdPriceBillBean);
    }

    @ApiOperation(value = "新增产品调价单")
    @PostMapping("/add")
    public ResponseHelper add(
            @ApiParam(required = true, value = "产品调价单") @RequestBody @Validated ProductUpdPriceBillBean productUpdPriceBillBean
    ) throws Exception {
        String add = productUpdPriceBillLogic.add(productUpdPriceBillBean);
        return ResponseHelper.success(add);
    }

    @ApiOperation(value = "编辑产品调价单")
    @PostMapping("/edit")
    public ResponseHelper edit(
            @ApiParam(required = true, value = "产品调价单") @Validated(value = { EditGroup.class, Default.class }) @RequestBody ProductUpdPriceBillBean productUpdPriceBillBean
    ) throws Exception {
        productUpdPriceBillLogic.edit(productUpdPriceBillBean);
        return ResponseHelper.success();
    }

    @ApiOperation(value = "提审产品调价单")
    @PostMapping("/submit")
    public ResponseHelper submit(
            @ApiParam(required = true, value = "产品调价单id") @RequestParam List<String> ids
    ) throws Exception {
        productUpdPriceBillLogic.submit(ids);
        return ResponseHelper.success();
    }

    @ApiOperation(value = "撤消产品调价单")
    @PostMapping("/revoke")
    public ResponseHelper revoke(
            @ApiParam(required = true, value = "产品调价单id") @RequestParam String id
    ) throws Exception {
        productUpdPriceBillLogic.revoke(id);
        return ResponseHelper.success();
    }

    @ApiOperation(value = "弃审产品调价单")
    @PostMapping("/review")
    public ResponseHelper review(
            @ApiParam(required = true, value = "产品调价单id") @RequestParam String id
    ) throws Exception {
        productUpdPriceBillLogic.review(id);
        return ResponseHelper.success();
    }

    @ApiOperation(value = "审核产品调价单")
    @PostMapping("/confirm")
    public ResponseHelper confirm(
            @ApiParam(required = true, value = "ids") @RequestParam List<String> ids,
            @ApiParam(required = true, value = "审核结果 OK=通过  FAIL=不通过") @RequestParam String examineStatus,
            @ApiParam(required = false, value = "审核备注") @RequestParam(required = false) String examineNote
    ) throws Exception {
        productUpdPriceBillLogic.confirm(ids,examineStatus,examineNote);
        return ResponseHelper.success();
    }

    @ApiOperation(value = "删除产品调价单")
    @PostMapping("/del")
    public ResponseHelper del(
            @ApiParam(required = true, value = "产品调价单") @RequestParam List<String> ids
    ) throws Exception {
        productUpdPriceBillLogic.del(ids);
        return ResponseHelper.success();
    }

    @GetMapping("/download")
    @ApiOperation("下载模板")
    public void download(
            HttpServletResponse response
    ) throws IOException {
        CommonUtils.downloadFile("调价单模板.xlsx",response);
    }

    @PostMapping("/material/import")
    @ApiOperation("导入调价单")
    public ResponseHelper<List<ProductUpdPriceBillItemBean>> importMaterial(
            MultipartFile file,
            @ApiParam(value = "单据类型 GENERAL=普通 HOME=家用工程 DIFF=收差", required = true)
            @RequestParam(defaultValue = "GENERAL") String billType
    ) throws Exception {
        List<Object> objects = ExcelUtils.importExcel(file);
        return ResponseHelper.success(productUpdPriceBillLogic.importData(objects, billType));
    }

    @ApiOperation(value = "产品调价单列表 -导出")
    @GetMapping("/export")
    public void export(
            @ApiParam(value = "金蝶编码") @RequestParam(required = false) String materialOldNumber,
            @ApiParam(value = "物料编码") @RequestParam(required = false) String materialNumber,
            @ApiParam(value = "物料名称") @RequestParam(required = false) String materialName,
            @ApiParam(value = "备注") @RequestParam(required = false) String remark,
            @ApiParam(value = "品类id") @RequestParam(required = false) String mainId,
            @ApiParam(value = "销售类型",required = false) @RequestParam(required = false) String saleTypeId,
            @ApiParam(value = "规格",required = false) @RequestParam(required = false) String specification,
            @ApiParam(value = "单据类型",required = false) @RequestParam(required = false, defaultValue = "GENERAL") String billType,
            @ApiParam(value = "生效日期") @RequestParam(required = false) String startDate,
            @ApiParam(value = "物料分类编号",required = false) @RequestParam(required = false) String k3CategoryNumber,
            HttpServletRequest request, HttpServletResponse response
    ) throws Exception {
        IPage<ProductUpdPriceBillListBean> page = productUpdPriceBillLogic.listPage(materialOldNumber,materialNumber, materialName,remark,mainId, saleTypeId,startDate, specification, billType, k3CategoryNumber,1, -1);
        ExcelData excelData = productUpdPriceBillLogic.export(page.getRecords());
        ExcelUtils.exportExcel(request,response,"产品调价单.xlsx",excelData);
    }

}
