package com.ho.flow.controller;

import com.ho.common.tools.constant.ContextConstant;
import com.ho.common.tools.constant.RedisKeyConstant;
import com.ho.common.tools.entity.SimpleUser;
import com.ho.common.tools.exception.BaseResponseCode;
import com.ho.common.tools.exception.BusinessException;
import com.ho.common.tools.exception.DataResult;
import com.ho.common.tools.service.RedisService;
import com.ho.common.tools.util.PageResult;
import com.ho.flow.service.WorkOrderPictureService;
import com.ho.flow.service.WorkOrderService;
import com.ho.flow.vo.req.workorder.WorkOrderAddReqVO;
import com.ho.flow.vo.req.workorder.WorkOrderPageReq;
import com.ho.flow.vo.req.workorder.WorkOrderUpdateReqVO;
import com.ho.flow.vo.resp.workorder.PictureRespVO;
import com.ho.flow.vo.resp.workorder.WorkOrderRespVO;
import com.ho.common.tools.entity.OrderDept;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;


/**
 * @Description 工单业务
 * Author yule
 * Date 2023/2/4 12:54
 */
@RestController
@RequestMapping(ContextConstant.FLOW_CONTEXT + "workOrder")
@Slf4j
@Api(tags = "工单查询")
public class WorkOrderController {
    @Autowired
    private RedisService redisService;

    @Autowired
    WorkOrderService workOrderService;

    @Autowired
    WorkOrderPictureService pictureService;


    @PostMapping("add")
    @ApiOperation(value = "新增工单业务数据")
    public DataResult add(@RequestBody @Valid WorkOrderAddReqVO vo) {
        int i = workOrderService.add(vo);
        if (i > 0) {
            return DataResult.success();
        } else {
            throw new BusinessException(BaseResponseCode.WORK_ORDER_ADD_ERROR);
        }
    }

    @PostMapping("delete")
    @ApiOperation(value = "删除工单业务数据")
    public DataResult delete(@RequestBody List<Long> ids) {

        int i = workOrderService.delete(ids);

        //删除电站相关图片数据
        pictureService.deletedByWorkOrderIds(ids);
        if (i > 0) {
            return DataResult.success();
        } else {
            throw new BusinessException(BaseResponseCode.WORK_ORDER_DELETE_ERROR);
        }
    }

    @PostMapping("update")
    @ApiOperation(value = "修改工单业务数据")
    public DataResult update(@RequestBody @Valid WorkOrderUpdateReqVO vo) {
        //更新图片
        pictureService.updatePicture(vo);
        int i = workOrderService.update(vo);
        if (i > 0) {
            return DataResult.success();
        } else {
            throw new BusinessException(BaseResponseCode.WORK_ORDER_UPDATE_ERROR);
        }
    }

    @PostMapping("getOne")
    @ApiOperation(value = "获取单个工单业务数据")
    public DataResult<WorkOrderRespVO> getOne(@RequestBody Long id) {
        //获取工单业务对象
        WorkOrderRespVO vo = workOrderService.getOne(id);
        //查询工单业务相关的图片信息
        List<PictureRespVO> pictureRespVOS = pictureService.selectByOrderId(id);
        vo.setPictureResps(pictureRespVOS);
        return DataResult.success(vo);
    }

    @PostMapping("page")
    @ApiOperation(value = "分页查询工单业务数据")
    public DataResult<PageResult<WorkOrderRespVO>> page(@RequestBody @Valid WorkOrderPageReq vo) {
        PageResult<WorkOrderRespVO> page = workOrderService.getPage(vo);
        return DataResult.success(page);
    }

    @PostMapping("orderUser")
    @ApiOperation(value = "查询流转可操作的人员")
    public DataResult<List<OrderDept>> selectOrderUser(HttpServletRequest request, @RequestBody List<String> postCodes) {
        String token = request.getHeader(RedisKeyConstant.User.ACCESS_TOKEN);
        String userTokenKey = RedisKeyConstant.User.TOKEN + token;
        //List<Integer> depts = new ArrayList<>();
        Integer groupId = null;
        //根据用户的登陆方式(账号还是手机号),延长对应缓存
        if (redisService.hasKey(userTokenKey)) {
            SimpleUser simpleUser = (SimpleUser) redisService.getSimpleUserByToken(token);
           //depts = simpleUser.getDepts();
            groupId = simpleUser.getGroupId();
        }

        List<OrderDept> orderDeptList = workOrderService.selectOrderUser(groupId, postCodes);
        return DataResult.success(orderDeptList);
    }

}
