package com.gyee.frame.controller.ticket;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.gson.JsonObject;
import com.gyee.frame.common.dataSources.DataSource;
import com.gyee.frame.common.dataSources.DataSourceType;
import com.gyee.frame.common.domain.AjaxResult;
import com.gyee.frame.model.ticket.*;
import com.gyee.frame.service.ticket.*;
import com.gyee.frame.util.ticket.TicketUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author chenmh
 * @since 2020-06-01
 */
@RestController
@RequestMapping("/workticket")
public class WorkticketController {

    @Autowired
    WorkticketService workticketService;
    @Autowired
    WoworktaskService woworktaskService;
    @Autowired
    ShiroService shiroService;

    /**
     * 分页查询工作票
     *
     * @param page
     * @param type
     * @return
     */
    @DataSource(value = DataSourceType.TICKET)
    @GetMapping(value = "list")
    public AjaxResult getList(@RequestHeader(value = "token") String header,
                              @RequestParam("page") Optional<Long> page,
                              @RequestParam("type") Optional<String> type) {
        TokenUser token = shiroService.findToken(header);
        Long pageNum = page.orElse(1L);
        String ticketType = type.orElse("wind");

        IPage<Workticket> ticket = workticketService.selectPage(pageNum, ticketType, token.getDepartment());
        if (ticket == null)
            return AjaxResult.error(4004, "数据读取异常");

        return AjaxResult.successData(ticket);
    }

    /**
     * 根据 id 查询详情
     *
     * @param id
     * @return
     */
    @DataSource(value = DataSourceType.TICKET)
    @GetMapping(value = "item")
    public AjaxResult getTicketItem(@RequestParam("id") String id) {
        Workticket ticket = workticketService.getById(id);
        if (ticket == null) {
            return AjaxResult.error(4004, "数据读取异常");
        }

        if (!ticket.getWtickettype().contains("线路")){
            // 工作任务
            Woworktask task = woworktaskService.getTask(ticket.getWonum());
            if (task != null) {
                ticket.setWorkaddress(task.getAddress());
                ticket.setWorktask(task.getDescription2());
            }
        }

        return AjaxResult.successData(ticket);
    }


    /**
     * 编辑ticket
     *
     * @param data
     * @return
     */
    @DataSource(value = DataSourceType.TICKET)
    @PostMapping(value = "edit")
    public AjaxResult editWorkTicket(@RequestParam("data") String data) {
        JSONObject jsonObject = JSONObject.parseObject(data);
        Workticket workticket = JSON.toJavaObject(jsonObject, Workticket.class);
        if (workticket == null)
            return AjaxResult.error(4004, "数据更新失败");

        boolean result = workticketService.updateById(workticket);
        if (!result)
            return AjaxResult.error(4004, "数据更新失败");

        return AjaxResult.success("数据更新成功");
    }

}

