package com.gree.mall.manager.controller.websit;

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.websit.*;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.websit.WebsitPartsRetLogic;
import com.gree.mall.manager.logic.websit.WebsitSalesRetLogic;
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.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.List;

@Slf4j
@RestController
@Api(value = "配件新旧返还", tags = {"配件新旧返还"})
@RequestMapping(value = "/websitPartsRet", produces = "application/json; charset=utf-8")
public class WebsitPartsRetController {

    @Autowired
    private WebsitPartsRetLogic websitPartsRetLogic;


    @ZfireList
    @PostMapping("/list/pageV2")
    @ApiOperation(value = "配件新旧返还列表")
    public ResponseHelper<IPage<WebsitPartsRetVO>> pageV2(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<WebsitPartsRetVO> page = websitPartsRetLogic.pageV2(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<WebsitPartsRetVO>() {});
    }

    @PostMapping("/pageExport")
    @ApiOperation(value = "配件新旧返还列表导出")
    public void listExport(@RequestBody ZfireParamBean zfireParamBean, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<WebsitPartsRetVO> page = websitPartsRetLogic.pageV2(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParam.getExportFields(), request, response);
    }

    @PostMapping("/add")
    @ApiOperation("创建旧件或新件")
    public ResponseHelper save(
            @RequestBody WebsitPartsRetAddUp websitPartsRetAddUp,
            HttpServletRequest request
    ) throws ParseException {
        websitPartsRetLogic.save(websitPartsRetAddUp);
        return ResponseHelper.success();
    }

    @PostMapping("/update")
    @ApiOperation("修改旧件或新件")
    public ResponseHelper update(
            @RequestBody WebsitPartsRetAddUp websitSalesRetAddUP,
            HttpServletRequest request
    ) throws Exception {
        websitPartsRetLogic.update(websitSalesRetAddUP);
        return ResponseHelper.success();
    }


    @PostMapping("/del")
    @ApiOperation("删除")
    public ResponseHelper del(
            @ApiParam(value = "页号", required = true) @RequestParam(required = true) List<String> partsId,
            HttpServletRequest request
    ) throws ParseException {
        websitPartsRetLogic.del(partsId);
        return ResponseHelper.success();
    }

    @PostMapping("/detail")
    @ApiOperation("旧件或新件的详情")
    public ResponseHelper<WebsitPartsRetDetail> detail(
            @ApiParam(value = "新旧件id" , required = true) @RequestParam(required = true) String id,

            HttpServletRequest request
    ) throws ParseException {
        WebsitPartsRetDetail  websitSalesRetDetail = websitPartsRetLogic.detail(id);
        return ResponseHelper.success(websitSalesRetDetail);
    }

}
