package com.hwrj.cloud.admin.controller;

import com.hwrj.cloud.admin.dto.UmsMemberOriginParam;
import com.hwrj.cloud.admin.dto.param.MemberWebPageParam;
import com.hwrj.cloud.admin.dto.param.SelectVo;
import com.hwrj.cloud.admin.excelEntity.UmsMemberOriginExcel;
import com.hwrj.cloud.admin.listener.ImportExcelEventListener;
import com.hwrj.cloud.admin.service.UmsMemberOriginService;
import com.hwrj.cloud.admin.util.CurrUserUtil;
import com.hwrj.cloud.admin.util.EasyExcelUtil;
import com.hwrj.cloud.common.api.CommonPage;
import com.hwrj.cloud.common.api.CommonResult;
import com.hwrj.cloud.common.exception.GlobalException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;

/**
 * @className:
 * @description: 生产地信息表
 * @author: lym
 * @date: 2020/9/5 10:38
 */
@RestController
@Api(description = "生产地信息管理")
@RequestMapping("/origin")
public class UmsMemberOriginController {

    @Autowired
    private UmsMemberOriginService umsMemberOriginService;
    @Autowired
    private ImportExcelEventListener importExcelEventListener;

    @ApiOperation(value = "导入生产地信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file",value = "导入文件",required = true,paramType = "MultipartFile" )
    })
    @GetMapping("/import")
    public CommonResult importExcel(@RequestParam("file") MultipartFile file, Principal principal){
        List<UmsMemberOriginExcel> listAll = new ArrayList<>();
        if(file != null){
            List<Object> list = null;
            try {
                list = EasyExcelUtil.readExcel(file, new UmsMemberOriginExcel(),2,2);
            } catch (IOException e) {
                //e.printStackTrace();
                throw new GlobalException(1,"上传文件错误!");
            }
            if(list != null && list.size() > 0){
                for(Object obj : list){
                    UmsMemberOriginExcel excel = (UmsMemberOriginExcel) obj;
                    excel.setCreateId(CurrUserUtil.getUserId());
                    excel.setState(0);
                    listAll.add(excel);
                }
            }
        }
        int saveNum = 0;
        if (listAll != null && listAll.size() > 0){
            int batchNum = 500;
            if (listAll.size() > batchNum){
                List<UmsMemberOriginExcel> list = listAll.subList(0,batchNum);
                saveNum = umsMemberOriginService.batchSave(list);
                //if (saveNum > 0){ //存在前面数据已存在情况
                    //异步导入
                    importExcelEventListener.batchSaveOriginExcel(listAll.subList(batchNum,listAll.size()));
                    return CommonResult.success(null,"正在导入数据，请稍后刷新!");
                //}
            }
            saveNum = umsMemberOriginService.batchSave(listAll);
            if (saveNum < 1){
                return CommonResult.success(null,"导入数据已存在，请勿重复导入");
            }
        }

        return CommonResult.success();
    }

    @ApiOperation(value = "查询列表",notes = "查询列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "origin",value = "生产地信息",required = true,paramType = "UmsMemberOriginProductParam")
    })
    @PostMapping("/list")
    public CommonResult list(@RequestBody MemberWebPageParam origin){
        List<UmsMemberOriginParam> list = umsMemberOriginService.list(origin);
        return CommonResult.success(CommonPage.restPage(list));
    }

    @ApiOperation(value = "生产地下拉列表",notes = "查询列表")
    @PostMapping("/list/select")
    public CommonResult listSelect(){
        List<SelectVo> list = umsMemberOriginService.listSelect(CurrUserUtil.getCompId());
        return CommonResult.success(list);
    }

    @ApiOperation(value = "新增生产地信息",notes = "新增生产地信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "origin",value = "生产地信息",required = true,paramType = "UmsMemberOriginProductParam")
    })
    @PostMapping("/save")
    public CommonResult save(@RequestBody @Valid UmsMemberOriginParam origin){
//        if (StringUtils.isEmpty(CurrUserUtil.getCompId())){
//            throw new GlobalException(1,"用户未绑定供应商不能添加生产地!");
//        }
//        origin.setCompId(CurrUserUtil.getCompId());
//        origin.setCompName(CurrUserUtil.getCompName());
//        origin.setCreateId(CurrUserUtil.getUserId());
        int i = umsMemberOriginService.save(origin);
        return CommonResult.success(i);
    }

    @ApiOperation(value = "修改生产地信息",notes = "修改生产地信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "origin",value = "生产地信息",required = true,paramType = "UmsMemberOriginProductParam")
    })
    @PostMapping("/update")
    public CommonResult update(@RequestBody @Valid UmsMemberOriginParam origin){
//        if (StringUtils.isEmpty(CurrUserUtil.getCompId())){
//            throw new GlobalException(1,"用户未绑定供应商不能编辑生产地!");
//        }
//        origin.setCompId(CurrUserUtil.getCompId());
//        origin.setCompName(CurrUserUtil.getCompName());
//        origin.setUpdateId(CurrUserUtil.getUserId());
        int i= umsMemberOriginService.update(origin);
        if (i > 0){
            return CommonResult.success(i);
        }
        return CommonResult.failed();
    }

    @ApiOperation(value = "查询生产地详细信息",notes = "查询生产地详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "主键",required = true,paramType = "Long")
    })
    @GetMapping("/findById")
    public CommonResult findById(@RequestParam("id") long id){
        UmsMemberOriginParam param = umsMemberOriginService.findById(id);
        return CommonResult.success(param);
    }

    @ApiOperation(value = "删除生产地信息",notes = "删除生产地信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "主键",required = true,paramType = "Long")
    })
    @GetMapping("/delete")
    public CommonResult delete(@RequestParam("id") long id){
        int i = umsMemberOriginService.delete(id);
        return CommonResult.success(i);
    }
}
