package com.xintong.visualinspection.controller.gettway;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xintong.system.err.BusinessException;
import com.xintong.visualinspection.bean.*;
import com.xintong.visualinspection.controller.BaseController;
import com.xintong.visualinspection.pojo.fwq.TaskData_F;
import com.xintong.visualinspection.service.CheckAppealService_F;
import com.xintong.visualinspection.service.RecordService_Q;
import com.xintong.visualinspection.service.TaskService_F;
import com.xintong.visualinspection.util.AuthorUtil;
import com.xintong.visualinspection.util.CacheUtil;
import com.xintong.visualinspection.util.Constants;
import org.springframework.web.bind.annotation.PathVariable;
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;

@RestController
@RequestMapping("/q/record")
public class RecordController_Q extends BaseController {

    @Reference(version = "1.0.0",
            application = "${dubbo.application.id}",
            registry = "${dubbo.registry.address}")
    private RecordService_Q recordService_q;



    @RequestMapping(value = "/getall/{page}/{size}")
    public String getall(HttpServletRequest request, @PathVariable Integer page, @PathVariable Integer size, @RequestBody QzdRecord qzdRecord) {
        User user = getCurrentUser(request);
        if (AuthorUtil.hasRole(user, Constants.ROLE_QZD_ADMIN)
            || AuthorUtil.hasRole(user, Constants.ROLE_QZD)
                || AuthorUtil.containsRole(user, "ROLE_QZD")) {
            qzdRecord.setDept(user.getOrganid());
        }
        Object ss = recordService_q.getAllRecord(page, size, qzdRecord);
        return super.returnSuccessResult(ss);
    }


    @RequestMapping(value = "/update")
    public String update(HttpServletRequest request, @Valid @RequestBody QzdRecord qzdRecord) {
        if (qzdRecord.getId() == null) {
            throw new BusinessException(20002);
        }
        User user = getCurrentUser(request);
        recordService_q.updateRecord(qzdRecord);
        QzdRecord t = recordService_q.getRecord(qzdRecord);
        return super.returnSuccessResult("修改成功", t);
    }

    @RequestMapping(value = "/add")
    public String addrecord(HttpServletRequest request, @Valid @RequestBody QzdRecord record) {
        if (record == null ) {
            throw new BusinessException(21502);
        }

        User user = getCurrentUser(request);
        record.setRecord_user(user.getId());
        try {
            recordService_q.addRecord(record);
        } catch (Exception e) {
            throw new BusinessException(21501);
        }

        return super.returnSuccessResult("添加成功", null);
    }


}
