package com.sw.common.accessory.server.controller;

import com.sw.common.accessory.server.dto.*;
import com.sw.common.accessory.server.entity.Accessory;
import com.sw.common.accessory.server.service.AccessoryService;
import com.sw.common.accessory.utils.ExcelUtil;
import com.sw.common.core.response.Result;
import com.sw.common.core.response.ResultGenerator;
import com.sw.common.core.utils.EasyExcelUtils;
import com.sw.common.mybatisplus.base.BaseAuController;
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.util.Assert;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;


/**
 * @author zhaowenjie
 */
@RestController
@RequestMapping("/accessory")
@Slf4j
@Api(tags = "辅材信息接口")
public class AccessoryController extends BaseAuController<AccessoryService, Accessory, AccessoryImportDto, AccessoryDto, AccessoryDto> {
    @Autowired
    private AccessoryService accessoryService;

    public AccessoryController() {
        super(Accessory.class, AccessoryDto.class, true, "辅材信息");
    }

    @ApiOperation(value = "批量对调")
    @PostMapping("/batchSwitching")
    public Result<String> upper(@RequestBody @Valid @ApiParam(value = "批量对调数据") BatchSwitch batchSwitch, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        accessoryService.batchSwitching(batchSwitch);
        return ResultGenerator.success();
    }

    @ApiOperation(value = "辅料已使用寿命追塑")
    @PostMapping("/life/{id}")
    public Result<AccessoryLife> life(@PathVariable @ApiParam(value = "辅材Id") Long id) {
        if (id == null) {
            return ResultGenerator.fail("辅材Id，不能为空");
        }
        AccessoryLife accessoryLife = accessoryService.life(id);
        return ResultGenerator.success(accessoryLife);
    }

    @ApiOperation(value = "辅材导入")
    @PostMapping("/import")
    public Result<String> importUser(MultipartFile file) {
        try {
            List<AccessoryImportDto> accessoryDtos = EasyExcelUtils.readExcel(file, new AccessoryImportDto(), 0, 2);
            Assert.isTrue(accessoryDtos.size() > 0, "导入数据不能为空");
            accessoryService.importAccessory(accessoryDtos);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("辅材导入错误：", e);
            return ResultGenerator.fail(e.getMessage());
        }
        return ResultGenerator.success();
    }

    @ApiOperation(value = "获取辅材导入记录模板")
    @GetMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "辅材导入模板", AccessoryImportDto.class, response);
    }
}