package cn.pluss.platform.controller.device;


import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.device.DeviceOperateInfoService;
import cn.pluss.platform.device.DeviceTransferService;
import cn.pluss.platform.dto.DeviceTransferDTO;
import cn.pluss.platform.entity.UserApp;
import cn.pluss.platform.userApp.UserAppService;
import cn.pluss.platform.util.StringUtil;
import cn.pluss.platform.vo.DeviceOperateInfoVO;
import cn.pluss.platform.vo.DeviceTransferVO;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 设备库存controller
 * </p>
 *
 * @author bzg
 * @since 2020-11-11
 */
@RestController
@RequestMapping("/device")
public class DeviceTransferController {

    @Resource
    private UserAppService userAppService;

    @Resource
    private DeviceTransferService dtfService;

    @Resource
    private DeviceOperateInfoService doiService;

    /**
     * 设备划拨接口
     *
     * @return
     */
    @PostMapping(value = "transfer")
    public Result<Object> transfer(@RequestBody DeviceTransferDTO dto) {
        dtfService.transfer(dto);
        return ResultGenerator.genSuccessResult("操作成功", null);
    }

    /**
     * 设备接收接口
     *
     * @param dto
     * @return
     */
    @PostMapping(value = "accept")
    public Result<Object> accept(@RequestBody DeviceTransferDTO dto) {
        dtfService.accept(dto);
        return ResultGenerator.genSuccessResult("操作成功", null);
    }


    /**
     * 划拨记录分页查询
     *
     * @param dto
     * @return
     */
    @GetMapping(value = "/transfer/page")
    public Result<Page<DeviceTransferVO>> page(Page<DeviceTransferVO> page, DeviceTransferDTO dto) {
        UserApp userApp = userAppService.checkApiToken();
        if (dto.getType().equals(DeviceTransferDTO.OUT)) {
            page.getOrders().add(OrderItem.desc("transferTime"));
        }

        if (dto.getType().equals(DeviceTransferDTO.GET)) {
            page.getOrders().add(OrderItem.desc("acceptTime"));
        }

        if (StringUtil.isEmpty(dto.getType()) || DeviceTransferDTO.OUT.equals(dto.getType())) {
            dto.setUserId(userApp.getUserId());
        } else {
            dto.setAcceptUserId(userApp.getUserId());
        }
        page = dtfService.pageInfo(page, dto);
        return ResultGenerator.genSuccessResult("获取成功", page);
    }

    @GetMapping(value = "/transfer/detail")
    public Result<Object> page(DeviceTransferDTO dto) {
        DeviceTransferVO vo = dtfService.getDetail(dto);
        return ResultGenerator.genSuccessResult("获取成功", vo);
    }

    /**
     * 获取设备流转详情
     *
     * @param dto
     * @return
     */
    @GetMapping(value = "/operate/info")
    public Result<Object> operateInfo(DeviceTransferDTO dto) {
        List<DeviceOperateInfoVO> list = doiService.selectListVoByDeviceNo(dto.getSnNo());
        return ResultGenerator.genSuccessResult("获取成功", list);
    }
}
