package com.casic.missiles.controller.meter;


import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.casic.missiles.core.base.controller.ExportController;
import com.casic.missiles.core.page.PageFactory;
import com.casic.missiles.dto.IdDTO;
import com.casic.missiles.dto.ReturnDTO;
import com.casic.missiles.dto.ReturnUtil;
import com.casic.missiles.dto.meter.MeterPriceImport;
import com.casic.missiles.dto.meter.MeterPriceRequest;
import com.casic.missiles.model.meter.MeterTraceSupplier;
import com.casic.missiles.model.meter.MeterTraceSupplierImport;
import com.casic.missiles.service.listeners.CommonExcelListener;
import com.casic.missiles.model.meter.MeterPrice;
import com.casic.missiles.service.meter.MeterPriceService;
import com.casic.missiles.utils.ConvertUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


@Api(tags = "价格库相关接口")
@RestController
@RequestMapping("/price")
@Slf4j
public class MeterPriceController extends ExportController {

    @Resource
    private MeterPriceService meterPriceService;

    /**
     * 价格库列表查询
     * @param meterPriceRequest
     * @return
     */
    @ApiOperation("价格库列表查询")
    @PostMapping("/queryPriceList")
    @ResponseBody
    public ReturnDTO<MeterPrice> queryPriceList(@RequestBody MeterPriceRequest meterPriceRequest) {
        Page<MeterPrice> page = PageFactory.defaultPage();
        Page<MeterPrice> list = meterPriceService.queryPriceList(page,meterPriceRequest);
        return ReturnUtil.success(list);
    }

    /**
     * 价格库列表删除
     * @return
     */
    @ApiOperation("价格库列表删除")
    @PostMapping("/deletePrice")
    @ResponseBody
    public ReturnDTO deletePrice(@RequestBody IdDTO idDto) {
        return meterPriceService.deletePrice(idDto);
    }

    /**
     * 价格库新增
     * @return
     */
    @ApiOperation("价格库新增")
    @PostMapping("/addMeterPrice")
    @ResponseBody
    public ReturnDTO addMeterPrice(@RequestBody MeterPrice meterPrice) {
        return meterPriceService.addMeterPrice(meterPrice);
    }

    @ApiOperation("批量导入价格库")
    @PostMapping(value = "/import", headers = "content-type=multipart/form-data")
    @ResponseBody
    public ReturnDTO importBatch(@RequestParam MultipartFile[] multipartFile) throws Exception {
        List<MeterPriceImport> sourceList = new ArrayList();
        for (MultipartFile file : multipartFile) {
            List<MeterPriceImport> list =  super.importExcel(MeterPriceImport.class, file, null, null);
            sourceList.addAll(list);
        }
        List<MeterPrice> targetList = ConvertUtils.sourceToTarget(sourceList, MeterPrice.class);
        return meterPriceService.addMeterPriceList(targetList);
    }

    /**
     * 价格详细信息
     * @return
     */
    @ApiOperation("价格详细信息")
    @PostMapping("/queryPriceInfo")
    @ResponseBody
    public ReturnDTO<MeterPrice> queryPriceInfo(@RequestBody IdDTO idDto) {
        MeterPrice meterPrice = meterPriceService.queryPriceInfo(idDto);
        return ReturnUtil.success(meterPrice);
    }

    /**
     * 价格信息编辑
     * @return
     */
    @ApiOperation("价格信息编辑")
    @PostMapping("/updatePriceInfo")
    @ResponseBody
    public ReturnDTO updatePriceInfo(@RequestBody MeterPrice meterPrice) {
        return meterPriceService.updatePriceInfo(meterPrice);
    }

    /**
     * 价格信息列表导出
     */
    @ApiOperation("价格信息列表导出")
    @PostMapping("/exportPriceList")
    @ResponseBody
    public void exportPriceList(@RequestBody MeterPriceRequest meterPriceRequest, HttpServletResponse response) {
        try {
            meterPriceService.exportPriceList(meterPriceRequest,response);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
