package com.imdroid.beidou.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.imdroid.beidou.common.HttpResult;
import com.imdroid.beidou.service.CommonExcelService;
import com.imdroid.secapi.dto.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@Controller
public class GnssResendController extends BasicController implements CommonExcelService<ResendRecord, ResendRecord> {
    @Autowired
    GnssGroupFwdMapper gnssGroupFwdMapper;
    @Autowired
    ResendRecordMapper resendRecordMapper;
    @Autowired
    TenantMapper tenantMapper;
    @Autowired
    OpLogManager opLogManager;

    /********* 推送页面 *********/
    @RequestMapping("/page/resend_records")
    public String fwdRecords(Model m, HttpSession session) {
        initModel(m, session);
        //以下用于下拉框数据
        List<GnssGroupFwd> gnssGroupFwds = gnssGroupFwdMapper.selectList(null);
        m.addAttribute("gnss_group_fwd_list", gnssGroupFwds);

        return "/page/resend_records";
    }
    @RequestMapping("/page/table/resend_record_add")
    public String gnssAddDev(Model m, HttpSession session) {
        initModel(m, session);
        //以下用于下拉框数据
        List<Tenant> tenants = tenantMapper.selectList(null);
        List<GnssGroupFwd> gnssGroupFwds = gnssGroupFwdMapper.selectList(null);

        m.addAttribute("tenant_list", tenants);
        m.addAttribute("gnss_group_fwd_list", gnssGroupFwds);
        m.addAttribute("device", new GnssDevice());

        return "/page/table/resend_record_add";
    }

    /********* 推送数据 *********/
    @RequestMapping("/fwd/resend_records")
    @ResponseBody
    public JSONObject listRecords(HttpSession session, Integer page, Integer limit, String searchParams) {
        return this.pageList(session, page, limit, searchParams);
    }

    @PostMapping("/fwd/resend_records/add")
    @ResponseBody
    public String update(HttpSession session, @RequestBody JSONObject object) throws Exception {
        // 从请求参数中创建对象
        ResendRecord resendRecord = JSONObject.toJavaObject(object,ResendRecord.class);
        resendRecord.setCreatetime(LocalDateTime.now());
        resendRecord.setState(ResendRecord.STATE_BREAK_POINT);
        int num = resendRecordMapper.insert(resendRecord);
        if (num == 0) {
            return HttpResult.failed();
        } else {
            opLogManager.addLog(getLoginUser(session),getTenantId(session),
                    OpLogManager.OP_TYPE_ADD,
                    OpLogManager.OP_OBJ_FWD_RECORD,
                    "create new resend record");
            return HttpResult.ok();
        }
    }

    @PostMapping("/fwd/resend_records/delete")
    @ResponseBody
    public String delete(HttpSession session, @RequestParam String del_id) throws Exception {
        int num = resendRecordMapper.deleteById(del_id);
        opLogManager.addLog(getLoginUser(session),getTenantId(session),
                OpLogManager.OP_TYPE_DEL,
                OpLogManager.OP_OBJ_FWD_RECORD,
                del_id + " deleted");
        if (num == 0) {
            return HttpResult.failed();
        } else{
            return HttpResult.ok();
        }
    }

    /**
     * 获取实体类的class
     *
     * @return 实体类的class
     */
    @Override
    public Class<ResendRecord> getEntityClass() {
        return ResendRecord.class;
    }

    /**
     * 获取实体类对应的mybatis mapper
     *
     * @return 实体类对应的mybatis mapper
     */
    @Override
    public BaseMapper<ResendRecord> getMapper() {
        return resendRecordMapper;
    }
}
