package cn.com.ty.lift.business.library.controller;

import cn.com.ty.lift.business.library.dao.entity.model.LiftImportModel;
import cn.com.ty.lift.business.library.dao.entity.model.request.LiftImportRequest;
import cn.com.ty.lift.business.library.service.LiftService;
import cn.com.ty.lift.common.utils.ValuePool;
import cn.com.ty.lift.common.verify.Validate;
import cn.com.ty.lift.common.verify.Validation;
import cn.com.ty.lift.common.verify.Val;
import cn.com.xwy.boot.web.dto.RestResponse;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *     电梯导入
 * </p>
 * @author wcz
 * @since  2019/12/27
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("lift")
public class LiftImportController {

    private LiftService liftService;

    @PostMapping("import")
    @Validation(fields = {"file","mtCompanyId", "projectId"})
    public RestResponse importData(@Val @RequestBody LiftImportRequest request) throws Exception{
        Long mtCompanyId = request.getMtCompanyId();
        MultipartFile file = request.getFile();
        Long projectId = request.getProjectId();
        // 1 检查文件有效性
        // 获取文件名，带后缀
        String originalFilename = file.getOriginalFilename();
        // 获取文件的后缀格式
        String fileSuffix = originalFilename.substring(originalFilename.lastIndexOf(ValuePool.DOT)).toLowerCase();
        Validate.isTrue(StrUtil.equalsAny(fileSuffix, ValuePool.EXCEL_IMPORT_FORMATS), ValuePool.EXCEL_IMPORT_FORMAT_SUPPORT);

        // 2 解析文件，转换成bean
        String filename = IdWorker.getIdStr() + fileSuffix;
        log.info("上传电梯表格文件，临时文件名: {}",filename);
        File excel = new File(filename);
        file.transferTo(excel);
        ExcelReader reader = ExcelUtil.getReader(excel);
        List<LiftImportModel> liftImportModels = reader.read(0,2,reader.getRowCount(),LiftImportModel.class);

        //解析完成后删除临时文件
        FileUtil.del(excel);
        Validate.isTrue(Objects.nonNull(liftImportModels) && liftImportModels.size() > 2,ValuePool.EXCEL_IMPORT_DATA_MISSING);
        Validate.notTrue(liftImportModels.size() > ValuePool.EXCEL_IMPORT_MAX_ROWS_ONETIME, ValuePool.EXCEL_IMPORT_MAX_ROWS_DESC);
        return liftService.assemble(liftImportModels,mtCompanyId,projectId);
    }

    @PostMapping("parse")
    public RestResponse parse(){
        File file = new File("d:/tyu/电梯导入模板1.xlsx");
        ExcelReader reader = ExcelUtil.getReader(file);
        List<LiftImportModel> liftImportModels = reader.read(0,2,reader.getRowCount(),LiftImportModel.class);

        return liftService.assemble(liftImportModels,1l,1l);
    }

    /**
     * 刷电梯旧数据，相同注册代码的电梯保留一条记录，其他关联表中的lift_id，同步刷
     * @return
     */
    @PostMapping("clearData")
    public RestResponse clearData(){
        boolean result = liftService.clearData();
        return RestResponse.success(result);
    }
}
