package com.ruoyi.web.controller.system;

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.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.system.domain.SysDeptfile;
import com.ruoyi.system.domain.TServiceManage;
import com.ruoyi.system.domain.dto.TUserProfileDto;
import com.ruoyi.system.service.ISysDeptfileService;
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.TUserProfile;
import com.ruoyi.system.service.ITUserProfileService;
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-25
 */
@RestController
@RequestMapping("/system/profile")
public class TUserProfileController extends BaseController
{
    @Autowired
    private ITUserProfileService tUserProfileService;
    @Autowired
    private ServerConfig serverConfig;
    @Autowired
    private ISysDeptfileService sysDeptfileService;

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

    /**
     * 查询用户档案列表
     */
    @PreAuthorize("@ss.hasPermi('system:profile:list')")
    @GetMapping("/list")
    public TableDataInfo list(TUserProfile tUserProfile)
    {
        startPage();
        List<TUserProfile> list = tUserProfileService.selectTUserProfileList(tUserProfile);
        return getDataTable(list);
    }

    /**
     * 查询用户档案列表
     */
    @GetMapping("/listDevice")
    public TableDataInfo listDevice(TUserProfileDto tUserProfileDto)
    {
        startPage();
        List<TUserProfileDto> list = tUserProfileService.selectTUserProfileListByDevice(tUserProfileDto);
        return getDataTable(list);
    }

    /**
     * 导出用户档案列表
     */
    @PreAuthorize("@ss.hasPermi('system:profile:export')")
    @Log(title = "用户档案", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TUserProfile tUserProfile)
    {
        List<TUserProfile> list = tUserProfileService.selectTUserProfileList(tUserProfile);
        ExcelUtil<TUserProfile> util = new ExcelUtil<TUserProfile>(TUserProfile.class);
        util.exportExcel(response, list, "用户档案数据");
    }

    /**
     * 获取用户档案详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:profile:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(tUserProfileService.selectTUserProfileById(id));
    }

    /**
     * 新增用户档案
     */
    @PreAuthorize("@ss.hasPermi('system:profile:add')")
    @Log(title = "用户档案", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TUserProfile tUserProfile)
    {
        if (StringUtils.isNotNull(mapUrl)){
            tUserProfile.setPhoto(mapUrl.get("photoUrl"));
            tUserProfile.setOtherPhoto(mapUrl.get("otherPhotoUrl"));
        }
        return toAjax(tUserProfileService.insertTUserProfile(tUserProfile));
    }

    /**
     * 资料上传请求(单个)
     * @return 集合
     */
    @PostMapping("/photoUpload")
    @ResponseBody
    public AjaxResult photoUploadFile(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 photoId = sysDeptfile.getId();
            map.put("photoId",photoId);
            mapUrl.put("photoUrl",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("/otherPhotoUpload")
    @ResponseBody
    public AjaxResult otherPhotoUploadFile(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 otherPhotoId = sysDeptfile.getId();
            map.put("otherPhotoId",otherPhotoId);
            mapUrl.put("otherPhotoUrl",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());
        }
    }

    /**
     * 修改用户档案
     */
    @PreAuthorize("@ss.hasPermi('system:profile:edit')")
    @Log(title = "用户档案", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TUserProfile tUserProfile)
    {
        return toAjax(tUserProfileService.updateTUserProfile(tUserProfile));
    }

    /**
     * 删除用户档案
     */
    @PreAuthorize("@ss.hasPermi('system:profile:remove')")
    @Log(title = "用户档案", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(tUserProfileService.deleteTUserProfileByIds(ids));
    }


    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<TUserProfile> util = new ExcelUtil<TUserProfile>(TUserProfile.class);
        util.importTemplateExcel(response, "用户档案模板");
    }

    /**
     * 导入
     */
    @Log(title = "用户档案", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<TUserProfile> util = new ExcelUtil<TUserProfile>(TUserProfile.class);
        List<TUserProfile> tUserProfileList = util.importExcel(file.getInputStream());
        String message = tUserProfileService.importTUserProfile(tUserProfileList);
        return AjaxResult.success(message);
    }
}
