package digital.laboratory.platform.entrustment.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import digital.laboratory.platform.common.core.util.R;
import digital.laboratory.platform.common.log.annotation.SysLog;
import digital.laboratory.platform.common.mybatis.security.service.DLPUser;
import digital.laboratory.platform.entrustment.entity.Taker;
import digital.laboratory.platform.entrustment.service.TakerService;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.web.bind.annotation.*;

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


/**
 * 采集员
 *
 * @author Zhang Xiaolong created at  2022-08-25
 * @describe 采集员 前端控制器
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/papp/entrustment/taker" )
@Api(value = "taker", tags = "007-采集员管理")
public class TakerController {

    private final  TakerService takerService;

	/**
	 * 通过id查询采集员
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}" )
//	@PreAuthorize("@pms.hasPermission('entrustment_taker_get')" )
	public R getById(@PathVariable("id" ) String id, HttpServletRequest theHttpServletRequest) {
		Principal principal = theHttpServletRequest.getUserPrincipal();
		DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

		Taker taker = takerService.getById(id);
		if (taker != null) {
			if (StrUtil.equalsIgnoreCase(dlpUser.getId(), taker.getOwnerUserId())) {
				return R.ok(taker);
			}
			else {
				return R.failed(String.format("你没有权限访问id为 %s 的采集员的数据", id));
			}
		}
		else {
			return R.failed(String.format("没有找到id为 %s 的采集员", id));
		}


	}


	/**
	 * 列表查询
	 * @param taker – 采集员
	 * @return
	 */
	@ApiOperation(value = "列表查询", notes = "列表查询")
	@GetMapping("/list" )
//	@PreAuthorize("@pms.hasPermission('entrustment_deliverer_get')" )
	public R getDelivererList(Taker taker, HttpServletRequest theHttpServletRequest) {
		Principal principal = theHttpServletRequest.getUserPrincipal();
		DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();
		taker.setOwnerUserId(dlpUser.getId());

		List<Taker> list = takerService.list(Wrappers.<Taker>query()
				.eq("owner_user_id", dlpUser.getId())       // 只查询当前用户拥有的采集员
				.like(StrUtil.isNotBlank(taker.getName()), "name", taker.getName())
				.orderByDesc("name")
		);
		return R.ok(list);
	}

	/**
     * 分页查询
     * @param page 分页对象
     * @param taker 采集员
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
//    @PreAuthorize("@pms.hasPermission('entrustment_taker_get')" )
    public R getTakerPage(Page page, Taker taker, HttpServletRequest theHttpServletRequest) {
	    Principal principal = theHttpServletRequest.getUserPrincipal();
	    DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();
	    taker.setOwnerUserId(dlpUser.getId());

	    return R.ok(takerService.page(page, Wrappers.<Taker>query()
			    .eq("owner_user_id", dlpUser.getId())       // 只查询当前用户拥有的采集员
			    .like(StrUtil.isNotBlank(taker.getName()), "name", taker.getName())
			    .orderByDesc("name")
	    ));
    }


    /**
     * 新增采集员
     * @param taker 采集员
     * @return R
     */
    @ApiOperation(value = "新增采集员", notes = "新增采集员")
    @SysLog("新增采集员" )
    @PostMapping
//    @PreAuthorize("@pms.hasPermission('entrustment_taker_add')" )
    public R postAddObject(@RequestBody Taker taker, HttpServletRequest theHttpServletRequest) {
	    Principal principal = theHttpServletRequest.getUserPrincipal();
	    DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();
	    taker.setOwnerUserId(dlpUser.getId());

		taker.setId(IdWorker.get32UUID().toUpperCase());
	    return R.ok(takerService.save(taker));
    }

    /**
     * 修改采集员
     * @param taker 采集员
     * @return R
     */
    @ApiOperation(value = "修改采集员", notes = "修改采集员")
    @SysLog("修改采集员" )
    @PutMapping
//    @PreAuthorize("@pms.hasPermission('entrustment_taker_edit')" )
    public R putUpdateById(@RequestBody Taker taker, HttpServletRequest theHttpServletRequest) {
	    Principal principal = theHttpServletRequest.getUserPrincipal();
	    DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

	    Taker oldTaker = takerService.getById(taker.getId());
	    if (oldTaker != null) {
		    if (StrUtil.equalsIgnoreCase(dlpUser.getId(), oldTaker.getOwnerUserId())) {
			    taker.setOwnerUserId(dlpUser.getId());
			    return R.ok(takerService.updateById(taker));
		    }
		    else {
			    return R.failed(String.format("你没有权限修改id为 %s 的采集员的数据", taker.getId()));
		    }
	    }
	    else {
		    return R.failed(String.format("没有找到id为 %s 的采集员", taker.getId()));
	    }
    }

    /**
     * 通过id删除采集员
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除采集员", notes = "通过id删除采集员")
    @SysLog("通过id删除采集员" )
    @DeleteMapping("/{id}" )
//    @PreAuthorize("@pms.hasPermission('entrustment_taker_del')" )
    public R deleteById(@PathVariable String id, HttpServletRequest theHttpServletRequest) {
	    Principal principal = theHttpServletRequest.getUserPrincipal();
	    DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();

	    Taker oldTaker = takerService.getById(id);
	    if (oldTaker != null) {
		    if (StrUtil.equalsIgnoreCase(dlpUser.getId(), oldTaker.getOwnerUserId())) {
			    if (takerService.removeById(id)) {
				    return R.ok(oldTaker, "采集员删除成功");
			    }
			    else {
				    return R.failed(oldTaker, "采集员删除失败");
			    }
		    }
		    else {
			    return R.failed(String.format("你没有权限删除id为 %s 的采集员", id));
		    }
	    }
	    else {
		    return R.failed(String.format("没有找到id为 %s 的采集员", id));
	    }


    }

}
