package com.ho.flow.controller;

import com.github.pagehelper.PageInfo;
import com.ho.common.tools.annotation.LogAnnotation;
import com.ho.common.tools.constant.CommonConstant;
import com.ho.common.tools.constant.ContextConstant;
import com.ho.common.tools.constant.RedisKeyConstant;
import com.ho.common.tools.entity.OrderDept;
import com.ho.common.tools.entity.SimpleUser;
import com.ho.common.tools.exception.DataResult;
import com.ho.common.tools.service.RedisService;
import com.ho.common.tools.util.PageResult;
import com.ho.common.tools.util.PageUtils;
import com.ho.flow.service.WorkOrderPlanService;
import com.ho.flow.service.WorkOrderService;
import com.ho.flow.vo.req.workorderplan.WorkOrderPlanAddReqVO;
import com.ho.flow.vo.req.workorderplan.WorkOrderPlanReqVO;
import com.ho.flow.vo.resp.workorderplan.WorkOrderPlanRespVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description 工单计划
 * Author yule
 * Date 2023/3/16 15:28
 */
@RequestMapping(ContextConstant.FLOW_CONTEXT + "workOrderPlan")
@RestController
@Api(tags = "工单计划")
public class WorkOrderPlanController {

    @Autowired
    WorkOrderPlanService workOrderPlanService;

    @Autowired
    RedisService redisService;

    @Autowired
    WorkOrderService workOrderService;

    //执行工单的岗位
    @Value("#{'${flow.workOrder.doing}'.split(',')}")
    String[] doing;

    @PostMapping("getPersons")
    @ApiOperation(value = "选择计划的执行人员列表")
    public DataResult<List<OrderDept>> getPersons(HttpServletRequest request) {
        String token = request.getHeader(CommonConstant.ACCESS_TOKEN);
        SimpleUser simpleUser = redisService.getSimpleUserByToken(token);
        List<String> postCodes = Arrays.asList(doing);
        List<OrderDept> orderDeptUsers = workOrderService.selectOrderUser(simpleUser.getGroupId(), postCodes);
        return DataResult.success(orderDeptUsers);
    }

    @PostMapping("add")
    @ApiOperation(value = "新增工单计划")
    @LogAnnotation(title = "工单计划", action = "新增工单计划")
    public DataResult add(@RequestBody @Valid WorkOrderPlanAddReqVO vo, HttpServletRequest request) {
        //根据登录人设置集团id和用户id
        String token = request.getHeader(RedisKeyConstant.User.ACCESS_TOKEN);
        String userTokenKey = RedisKeyConstant.User.TOKEN + token;
        if (redisService.hasKey(userTokenKey)) {
            SimpleUser simpleUser = redisService.getSimpleUserByToken(token);
            vo.setGroupId(simpleUser.getGroupId());
            vo.setUserId(simpleUser.getUserId());
        }
        workOrderPlanService.add(vo);
        return DataResult.success();
    }

    @PostMapping("delete")
    @ApiOperation(value = "删除工单计划")
    @LogAnnotation(title = "工单计划", action = "删除工单计划")
    public DataResult delete(@RequestBody @Valid WorkOrderPlanReqVO vo) {
        workOrderPlanService.delete(vo);
        return DataResult.success();
    }

    @PostMapping("update")
    @ApiOperation(value = "修改工单计划")
    @LogAnnotation(title = "工单计划", action = "修改工单计划")
    public DataResult update(@RequestBody @Valid WorkOrderPlanAddReqVO vo) {
        workOrderPlanService.update(vo);
        return DataResult.success();
    }

    @PostMapping("all")
    @ApiOperation(value = "查询全部工单计划")
    public DataResult<PageResult<WorkOrderPlanRespVO>> getAll(@RequestBody @Valid WorkOrderPlanReqVO vo,HttpServletRequest request) {
        //根据登录人设置集团id和用户id
        String token = request.getHeader(RedisKeyConstant.User.ACCESS_TOKEN);
        String userTokenKey = RedisKeyConstant.User.TOKEN + token;
        if (redisService.hasKey(userTokenKey)) {
            SimpleUser simpleUser = redisService.getSimpleUserByToken(token);
            vo.setGroupId(simpleUser.getGroupId());
        }
        List<WorkOrderPlanRespVO> workOrderPlanRespVOList = workOrderPlanService.getAll(vo);
        //根据时间排序
        workOrderPlanRespVOList = workOrderPlanRespVOList.stream().sorted(
                Comparator.comparing(WorkOrderPlanRespVO::getEffectDate, Comparator.nullsLast(Date::compareTo)).reversed()
        ).collect(Collectors.toList());
        PageResult pageResult = new PageResult<>();
        if (!workOrderPlanRespVOList.isEmpty()) {
            List list = PageUtils.dealList(workOrderPlanRespVOList, vo.getPageNum(), vo.getPageSize());
            pageResult = PageUtils.getPageResult(new PageInfo<>(list));
            pageResult.setTotalRows(workOrderPlanRespVOList.size());
            pageResult.setTotalPages(list.size() / vo.getPageSize() + 1);
        }
        return DataResult.success(pageResult);
    }

    @PostMapping("getOne")
    @ApiOperation(value = "查询单个工单计划")
    public DataResult<WorkOrderPlanRespVO> getOne(@RequestBody @Valid WorkOrderPlanReqVO vo) {
        WorkOrderPlanRespVO workOrderPlanRespVO = workOrderPlanService.getOne(vo);
        return DataResult.success(workOrderPlanRespVO);
    }
}
