package com.qmth.sop.server.api;

import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.sop.business.bean.params.DevicePlaceParam;
import com.qmth.sop.business.bean.result.DevicePlaceResult;
import com.qmth.sop.business.bean.result.DeviceSignResult;
import com.qmth.sop.business.entity.SysUser;
import com.qmth.sop.business.service.TBDeviceDeliveryService;
import com.qmth.sop.common.annotation.OperationLog;
import com.qmth.sop.common.contant.SystemConstant;
import com.qmth.sop.common.enums.DeviceStatusEnum;
import com.qmth.sop.common.enums.LogTypeEnum;
import com.qmth.sop.common.enums.MailTypeEnum;
import com.qmth.sop.common.util.Result;
import com.qmth.sop.common.util.ResultUtil;
import com.qmth.sop.common.util.ServletUtil;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@Api(tags = "设备管理Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_DEVICE_MANAGE)
public class DeviceDeliveryManageController {

    @Resource
    TBDeviceDeliveryService tbDeviceDeliveryService;

    @ApiOperation(value = "设备签收登记列表查询")
    @RequestMapping(value = "/sign/list", method = RequestMethod.POST)
    @ApiResponses({ @ApiResponse(code = 200, message = "返回信息", response = DeviceSignResult.class) })
    public Result signList(@ApiParam(value = "派单号") @RequestParam String crmNo) {
        return ResultUtil.ok(tbDeviceDeliveryService.signList(crmNo));
    }

    @ApiOperation(value="设备签收")
    @RequestMapping(value = "sign" , method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
    @OperationLog(logType = LogTypeEnum.UPDATE)
    public Result sign(@ApiParam(value = "设备发货id", required = true) @RequestParam String id) {
        SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
        tbDeviceDeliveryService.sign(SystemConstant.convertIdToLong(id) ,requestUser.getId());
        return ResultUtil.ok();
    }

    @ApiOperation(value="设备未签收")
    @RequestMapping(value = "unsigned" , method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
    @OperationLog(logType = LogTypeEnum.UPDATE)
    public Result unsigned(@ApiParam(value = "设备发货id", required = true) @RequestParam String id) {
        SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
        tbDeviceDeliveryService.unsigned(SystemConstant.convertIdToLong(id), requestUser.getId());
        return ResultUtil.ok();
    }


    @ApiOperation(value="设备签收编辑")
    @RequestMapping(value = "sign/edit" , method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
    @OperationLog(logType = LogTypeEnum.UPDATE)
    public Result signEdit(@ApiParam(value = "设备发货id", required = true) @RequestParam String id,
                           @ApiParam(value = "设备状态", required = true) @RequestParam DeviceStatusEnum deviceStatus) {
        SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
        tbDeviceDeliveryService.signEdit(SystemConstant.convertIdToLong(id), deviceStatus, requestUser.getId());
        return ResultUtil.ok();
    }

    @ApiOperation(value = "设备去处登记列表查询")
    @RequestMapping(value = "/place/list", method = RequestMethod.POST)
    @ApiResponses({ @ApiResponse(code = 200, message = "返回信息", response = DevicePlaceResult.class) })
    public Result placeList(@ApiParam(value = "派单号", required = true) @RequestParam String crmNo) {
        return ResultUtil.ok(tbDeviceDeliveryService.placeList(crmNo));
    }


    @ApiOperation(value="设备去处登记入库")
    @RequestMapping(value = "in" , method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
    @OperationLog(logType = LogTypeEnum.UPDATE)
    public Result in(@ApiParam(value = "设备发货id", required = true) @RequestParam String id,
                     @ApiParam(value = "入库方式", required = true) @RequestParam MailTypeEnum inMode,
                     @ApiParam(value = "快递单号:入库方式-选择邮寄不能为空", required = false) @RequestParam(required = false) String expressNo,
                     @ApiParam(value = "备注:入库方式-选择其他的时候不能为空", required = false) @RequestParam(required = false) String remark) {
        SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
        tbDeviceDeliveryService.in(SystemConstant.convertIdToLong(id), inMode, expressNo, remark, requestUser.getId());
        return ResultUtil.ok();
    }

    @ApiOperation(value="设备去处登记中转")
    @RequestMapping(value = "transfer" , method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
    @OperationLog(logType = LogTypeEnum.UPDATE)
    public Result transfer(@ApiParam(value = "设备发货id", required = true) @RequestParam String id,
                           @ApiParam(value = "中转方式", required = true) @RequestParam MailTypeEnum transferMode,
                           @ApiParam(value = "快递单号", required = false) @RequestParam(required = false) String expressNo,
                           @ApiParam(value = "接收方单号", required = true) @RequestParam String receiveCrmNo,
                           @ApiParam(value = "备注", required = false) @RequestParam(required = false) String remark) {
        SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
        tbDeviceDeliveryService.transfer(SystemConstant.convertIdToLong(id), transferMode, expressNo, receiveCrmNo, remark, requestUser.getId());
        return ResultUtil.ok();
    }

    @ApiOperation(value="设备去处编辑")
    @RequestMapping(value = "place/edit" , method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
    @OperationLog(logType = LogTypeEnum.UPDATE)
    public Result placeEdit(@Valid @ApiParam(value = "设备去处编辑信息", required = false) @RequestBody(required = false) DevicePlaceParam devicePlaceParam) {
        SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
        tbDeviceDeliveryService.devicePlaceEdit(devicePlaceParam, requestUser.getId());
        return ResultUtil.ok();
    }


}
