package com.hwrj.cloud.admin.controller;


import com.hwrj.cloud.admin.dto.UmsCompanyDto;
import com.hwrj.cloud.admin.dto.param.CompanyWebPageParam;
import com.hwrj.cloud.admin.dto.param.SelectNewVo;
import com.hwrj.cloud.admin.dto.param.SelectVo;
import com.hwrj.cloud.admin.excelEntity.UmsCompanyInfoExcel;
import com.hwrj.cloud.admin.model.UmsAdmin;
import com.hwrj.cloud.admin.service.UmsAdminService;
import com.hwrj.cloud.admin.service.UmsCompanyService;
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 io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.Model;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@Api(tags = "UmsCompanyController", description = "供应商信息")
@RequestMapping("/umsCompany")
@Slf4j
public class UmsCompanyController {


    @Autowired
    private UmsCompanyService umsCompanyService;

    @Autowired
    private UmsAdminService adminService;

    @ApiOperation(value = "添加供应商")
    @PutMapping("/info/add")
    @ResponseBody
    public CommonResult infoAdd(@RequestBody UmsCompanyDto dto,Principal principal){
        Long userId = getUserId(principal);

        dto.setCreateId(userId);
        umsCompanyService.add(dto);
        return CommonResult.success();
    }

    @ApiOperation(value = "修改供应商")
    @PutMapping("/info/update")
    @ResponseBody
    public CommonResult infoUpdate(@RequestBody UmsCompanyDto dto,Principal principal){
        Long userId = getUserId(principal);

        dto.setCreateId(userId);
        umsCompanyService.update(dto);
        return CommonResult.success();
    }

    @ApiOperation(value = "删除供应商")
    @GetMapping("/info/delete")
    @ResponseBody
    public CommonResult infoDelete(@RequestParam(value = "compId")Long compId,Principal principal){
        Long userId = getUserId(principal);

        umsCompanyService.delete(compId);
        return CommonResult.success();
    }

    @ApiOperation(value = "获取供应商列表")
    @GetMapping("/dt/list")
    @ResponseBody
    public CommonResult list(CompanyWebPageParam companyWebPageParam){

        Map<String,Object> obj= umsCompanyService.getCompanyInfoList(companyWebPageParam);
        return CommonResult.success(obj);
    }

    @ApiOperation(value = "获取供应商列表")
    @GetMapping("/select/list")
    @ResponseBody
    public CommonResult selectList(){

        List<SelectNewVo> list = umsCompanyService.selectList();
        return CommonResult.success(list);
    }

    @ApiOperation(value = "获取供应商列表")
    @GetMapping("/select/alllist")
    @ResponseBody
    public CommonResult alllist(){

        List<SelectVo> list = umsCompanyService.alllist();
        return CommonResult.success(list);
    }

    @ApiOperation(value = "获取供应商详细信息")
    @GetMapping("/dt/detail")
    @ResponseBody
    public CommonResult detail(@RequestParam(value = "compId")Long compId){

        UmsCompanyDto dto = umsCompanyService.getDetailCompany(compId);
        return CommonResult.success(dto);
    }




    /**
     * 导入excel
     * @param request
     * @param model
     * @param files
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "供应商导入")
    @RequestMapping(value = "excelImport", method = {RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public CommonResult excelImport(HttpServletRequest request, Model model, @RequestParam("uploadFile") MultipartFile[] files, Principal principal) throws Exception {
        Long userId = getUserId(principal);
        List<UmsCompanyInfoExcel> listAll = new ArrayList<>();
        if(files != null && files.length > 0){
            MultipartFile file = files[0];
            List<Object> list = EasyExcelUtil.readExcel(file, new UmsCompanyInfoExcel(),1,2);
            if(list != null && list.size() > 0){
                for(Object o : list){
                    UmsCompanyInfoExcel umsCompanyInfoExcel = (UmsCompanyInfoExcel) o;
                    umsCompanyInfoExcel.setCreateId(userId);
                    listAll.add(umsCompanyInfoExcel);
                }
            }
        }
        umsCompanyService.importExcel(listAll);

        return CommonResult.success(listAll);
    }


    public Long getUserId(Principal principal){
        Long userId = null;
        try {
            String username = principal.getName();

            if (!StringUtils.isEmpty(username)){
                UmsAdmin umsAdmin = adminService.getAdminByUsername(username);
                userId = umsAdmin.getId();
            }
        }catch (Exception e){
            log.info("获取用户id失败");
        }
        return userId;
    }

    public CommonResult deleteFile(@RequestParam(value = "id")Long id){

        return CommonResult.success();
    }



}
