package com.ruoyi.web.controller.system;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.system.domain.SysDeptfile;
import com.ruoyi.system.service.ISysDeptfileService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TServiceManage;
import com.ruoyi.system.service.ITServiceManageService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 服务商管理Controller
 * 
 * @author zhengjie
 * @date 2023-08-22
 */
@RestController
@RequestMapping("/system/manage")
public class TServiceManageController extends BaseController
{
    @Autowired
    private ITServiceManageService tServiceManageService;
    @Autowired
    private ServerConfig serverConfig;
    @Autowired
    private ISysDeptfileService sysDeptfileService;

    Map<Object,Integer> map = new HashMap<>();
    Map<String,String> mapUrl = new HashMap<>();

    /**
     * 查询服务商管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:manage:list')")
    @GetMapping("/list")
    public TableDataInfo list(TServiceManage tServiceManage)
    {
        startPage();
        List<TServiceManage> list = tServiceManageService.selectTServiceManageList(tServiceManage);
        return getDataTable(list);
    }

    /**
     * 导出服务商管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:manage:export')")
    @Log(title = "服务商管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TServiceManage tServiceManage)
    {
        List<TServiceManage> list = tServiceManageService.selectTServiceManageList(tServiceManage);
        ExcelUtil<TServiceManage> util = new ExcelUtil<TServiceManage>(TServiceManage.class);
        util.exportExcel(response, list, "服务商管理数据");
    }

    /**
     * 获取服务商管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:manage:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(tServiceManageService.selectTServiceManageById(id));
    }

    /**
     * 新增服务商管理
     */
    @PreAuthorize("@ss.hasPermi('system:manage:add')")
    @Log(title = "服务商管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TServiceManage tServiceManage) throws IOException {
        if (StringUtils.isNotNull(mapUrl)){
            tServiceManage.setBusinessurl(mapUrl.get("businessUrl"));
            tServiceManage.setContracturl(mapUrl.get("contractUrl"));
            tServiceManage.setStoreurl(mapUrl.get("storeUrl"));
        }
        return toAjax(tServiceManageService.insertTServiceManage(tServiceManage));
    }

    /**
     * 资料上传请求(单个)
     * @return 集合
     */
    @PostMapping("/businessUpload")
    @ResponseBody
    public AjaxResult businessUploadFile(MultipartFile file) throws Exception {
        try {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            //获取文件的上传路径
            String url = serverConfig.getUrl() + fileName;
            //得到文件的文件名
            String[] split = fileName.split("/");
            //
            String fname = split[split.length - 1];
            //新建一个附件类，把以下的数据赋值给它
            SysDeptfile sysDeptfile = new SysDeptfile();
            //设置文件的路径
            sysDeptfile.setFileUrl(url);
            //设置文件的name
            sysDeptfile.setFileName(fname);
            //设置文件的大小
            sysDeptfile.setFileSize(file.getSize());
            //设置文件的原始文件名称，即上传的文件名称
            sysDeptfile.setOriginalFilename(file.getOriginalFilename());
            //调用文件的新增方法，添加到数据库
            sysDeptfileService.insertSysDeptfile(sysDeptfile);
            //再添加到数据库后，得到了文件的id
            Integer businessId = sysDeptfile.getId();
            map.put("businessId",businessId);
            mapUrl.put("businessUrl",url);
            //创建一个ajax结果，这个ajax继承map集合，相当于一个map集合
            AjaxResult ajax = new AjaxResult();
            ajax.put("url",url);
            ajax.put("name", fname);
//            ajax.put("businessId",businessId);
//            ajax.put("businessFileName", fileName);

            return AjaxResult.success("插入成功",ajax);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 资料上传请求(单个)
     * @return 集合
     */
    @PostMapping("/storeUpload")
    @ResponseBody
    public AjaxResult storeUploadFile(MultipartFile file) throws Exception {
        try {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            //获取文件的上传路径
            String url = serverConfig.getUrl() + fileName;
            //得到文件的文件名
            String[] split = fileName.split("/");
            //
            String fname = split[split.length - 1];
            //新建一个附件类，把以下的数据赋值给它
            SysDeptfile sysDeptfile = new SysDeptfile();
            //设置文件的路径
            sysDeptfile.setFileUrl(url);
            //设置文件的name
            sysDeptfile.setFileName(fname);
            //设置文件的大小
            sysDeptfile.setFileSize(file.getSize());
            //设置文件的原始文件名称，即上传的文件名称
            sysDeptfile.setOriginalFilename(file.getOriginalFilename());
            //调用文件的新增方法，添加到数据库
            sysDeptfileService.insertSysDeptfile(sysDeptfile);
            //再添加到数据库后，得到了文件的id
            Integer storeId = sysDeptfile.getId();
            map.put("storeId",storeId);
            mapUrl.put("storeUrl",url);
            //创建一个ajax结果，这个ajax继承map集合，相当于一个map集合
            AjaxResult ajax = new AjaxResult();
            ajax.put("url",url);
            ajax.put("name", fname);
//            ajax.put("storeId",storeId);
//            ajax.put("storeFileName", fileName);

            return AjaxResult.success("插入成功",ajax);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 资料上传请求(单个)
     * @return 集合
     */
    @PostMapping("/contractUpload")
    @ResponseBody
    public AjaxResult contractUploadFile(MultipartFile file) throws Exception {
        try {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            //获取文件的上传路径
            String url = serverConfig.getUrl() + fileName;
            //得到文件的文件名
            String[] split = fileName.split("/");
            //
            String fname = split[split.length - 1];
            //新建一个附件类，把以下的数据赋值给它
            SysDeptfile sysDeptfile = new SysDeptfile();
            //设置文件的路径
            sysDeptfile.setFileUrl(url);
            //设置文件的name
            sysDeptfile.setFileName(fname);
            //设置文件的大小
            sysDeptfile.setFileSize(file.getSize());
            //设置文件的原始文件名称，即上传的文件名称
            sysDeptfile.setOriginalFilename(file.getOriginalFilename());
            //调用文件的新增方法，添加到数据库
            sysDeptfileService.insertSysDeptfile(sysDeptfile);
            //再添加到数据库后，得到了文件的id
            Integer contractId = sysDeptfile.getId();
            map.put("contractId",contractId);
            mapUrl.put("contractUrl",url);
            //创建一个ajax结果，这个ajax继承map集合，相当于一个map集合
            AjaxResult ajax = new AjaxResult();
            ajax.put("url",url);
            ajax.put("name", fname);
//            ajax.put("contractId",contractId);
//            ajax.put("contractFileName", fileName);

            return AjaxResult.success("插入成功",ajax);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 修改服务商管理
     */
    @PreAuthorize("@ss.hasPermi('system:manage:edit')")
    @Log(title = "服务商管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TServiceManage tServiceManage)
    {
        return toAjax(tServiceManageService.updateTServiceManage(tServiceManage));
    }

    /**
     * 删除服务商管理
     */
    @PreAuthorize("@ss.hasPermi('system:manage:remove')")
    @Log(title = "服务商管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(tServiceManageService.deleteTServiceManageByIds(ids));
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<TServiceManage> util = new ExcelUtil<TServiceManage>(TServiceManage.class);
        util.importTemplateExcel(response, "机构管理模板");
    }

    /**
     * 导入
     */
    @Log(title = "行政机构", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<TServiceManage> util = new ExcelUtil<TServiceManage>(TServiceManage.class);
        List<TServiceManage> tServiceManageList = util.importExcel(file.getInputStream());
        String message = tServiceManageService.importTServiceManage(tServiceManageList);
        return AjaxResult.success(message);
    }
}
