/*
 *    Copyright (c) 2018-2025, hnqz All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: hnqz
 */

package com.qunzhixinxi.hnqz.admin.controller;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qunzhixinxi.hnqz.admin.entity.WmImportUser;
import com.qunzhixinxi.hnqz.admin.service.WmImportUserService;
import com.qunzhixinxi.hnqz.common.core.util.R;
import com.qunzhixinxi.hnqz.common.log.annotation.SysLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


/**
 * 导入任务成员表
 *
 * @author pigx code generator
 * @date 2021-01-06 19:39:17
 */
@RestController
@AllArgsConstructor
@RequestMapping("/wmimportuser" )
@Api(value = "wmimportuser", tags = "导入任务成员表管理")
public class WmImportUserController {

    private final  WmImportUserService wmImportUserService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param wmImportUser 导入任务成员表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public R getWmImportUserPage(Page page, WmImportUser wmImportUser) {
		OrderItem orderItem=new OrderItem();
		orderItem.setColumn("create_time");
		orderItem.setAsc(false);
		page.addOrder(orderItem);
    	wmImportUser.setDelFlag("0");
        return R.ok(wmImportUserService.page(page, Wrappers.query(wmImportUser)));
    }


    /**
     * 通过id查询导入任务成员表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('admin_wmimportuser_view')" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(wmImportUserService.getById(id));
    }

    /**
     * 新增导入任务成员表
     * @param wmImportUser 导入任务成员表
     * @return R
     */
    @ApiOperation(value = "新增导入任务成员表", notes = "新增导入任务成员表")
    @SysLog("新增导入任务成员表" )
    @PostMapping
    public R save(@RequestBody WmImportUser wmImportUser) {
		try {
			return wmImportUserService.saveImportUser(wmImportUser);
		} catch (Exception e) {
			e.printStackTrace();
			return R.failed();
		}
    }

    /**
     * 修改导入任务成员表
     * @param wmImportUser 导入任务成员表
     * @return R
     */
    @ApiOperation(value = "修改导入任务成员表", notes = "修改导入任务成员表")
    @SysLog("修改导入任务成员表" )
    @PutMapping
    public R updateById(@RequestBody WmImportUser wmImportUser) {
        return R.ok(wmImportUserService.updateById(wmImportUser));
    }

    /**
     * 通过id删除导入任务成员表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除导入任务成员表", notes = "通过id删除导入任务成员表")
    @SysLog("通过id删除导入任务成员表" )
    @GetMapping("/del/{id}" )
    public R removeById(@PathVariable Integer id) {

    	return R.ok(wmImportUserService.delImportUser(id));
    }


	/**
	 * 导入任务成员
	 * @param file
	 * @param scoreId
	 * @return
	 */
	@ApiOperation(value = "导入任务成员", notes = "导入任务成员")
	@SysLog("导入任务成员" )
	@PostMapping("/impImportUser")
	public R impImportUser(@RequestPart("file")MultipartFile file, @RequestParam("scoreId") String scoreId){
		if(StringUtil.isEmpty(scoreId)){
			return R.failed("缺少参数：积分包id");
		}
		if(file.isEmpty()){
			return R.failed("请选择上传文件!");
		}

		try {
			return wmImportUserService.impImportUser(file,scoreId);
		} catch (Exception e) {
			e.printStackTrace();
			return R.failed("上传失败");
		}
	}
}
