package com.storlead.sales.controller.customer;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.storlead.framework.util.LoginUserUtil;
import com.storlead.framework.auth.vo.LoginUser;
import com.storlead.framework.common.result.Result;
import com.storlead.sales.pojo.dto.customer.WorkOrderAddDTO;
import com.storlead.sales.pojo.dto.customer.WorkOrderChageUserDTO;
import com.storlead.sales.pojo.dto.customer.WorkOrderSearchDTO;
import com.storlead.sales.pojo.vo.customer.WorkOrderDetailVO;
import com.storlead.sales.pojo.vo.customer.WorkOrderListVO;
import com.storlead.sales.service.customer.WorkOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.*;

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

/**
 * <p>
 * 工单表 前端控制器
 * </p>
 *
 * @author YPZ
 * @since 2024-04-29
 */
@RestController
@RequestMapping("/work-order")
@Api(tags = "销售: 工单管理相关接口")
public class WorkOrderController {
    @Resource
    private WorkOrderService workOrderService;

    @PostMapping(value = "/add")
    @ApiOperation(value = "工单管理-新增/修改工单", notes = "工单管理-新增/修改工单")
    public Result<?> add(@RequestBody WorkOrderAddDTO workOrderAddDTO) {

        return workOrderService.addWorkOrder(workOrderAddDTO);
    }

    /**
     * 分配工单、转他人处理接口
     * @return
     */
    @PostMapping("/distribution")
    @ApiOperation(value = "工单管理-新增/分配工单、转他人处理接口", notes = "工单管理-新增/分配工单、转他人处理接口")
    public Result distribution(@RequestBody WorkOrderChageUserDTO workOrderChageUserDTO) {
        LoginUser loginUser = LoginUserUtil.getLoginUser();
        workOrderChageUserDTO.setCreateBy(loginUser.getId());
//        workOrderVO.setCreateUser(loginUser.getRealName());
        return workOrderService.distribution(workOrderChageUserDTO);
    }
    /**
     * 已撤销/已完成/已确认/再次发起工单接口
     * @return
     */
    @PostMapping("/update-status")
    @ApiOperation(value = "工单管理-已撤销/已完成/已确认/再次发起工单接口", notes = "工单管理-已撤销/已完成/已确认/再次发起工单接口")
    public Result updateStatus(@RequestBody WorkOrderChageUserDTO workOrderChageUserDTO) {
        LoginUser loginUser = LoginUserUtil.getLoginUser();
        //此处把操作者用户姓名传递到service
        return workOrderService.status(workOrderChageUserDTO,loginUser.getId());
    }

    @PostMapping(value = "/list")
    @ApiOperation(value = "工单管理-列表", notes = "工单管理-列表")
    public Result<IPage<WorkOrderListVO>> list(@RequestBody WorkOrderSearchDTO workOrderSearchDTO) {
        return workOrderService.selectListVO(workOrderSearchDTO);
    }

    /**
     * 通过id查询
     *
     * @return
     */
    @GetMapping(value = "/get-by-id")
    @ApiOperation(value = "工单管理查询-通过id查看详情", notes = "工单管理查询-通过id查看详情")
    public Result<WorkOrderDetailVO> getById(@RequestParam(name="id",required=false) Long id) {

        WorkOrderDetailVO workOrderDetailVO = workOrderService.getWorkOrderVOById(id);
        return Result.result(workOrderDetailVO);
    }

    @ApiOperation(value = "工单管理-删除商机", notes = "工单管理-删除商机")
    @PostMapping("delete")
    public Result delete(Long id) {
        if(Objects.isNull(id)) {
            return Result.error("参数错误");
        }
        workOrderService.lgDelete(id);
        return Result.ok();
    }

}
