package com.imdroid.beidou.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imdroid.beidou.common.HttpResult;
import com.imdroid.beidou.entity.Tenant;
import com.imdroid.beidou.entity.TenantMapper;
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.ArrayList;
import java.util.List;

@Controller
public class TenantController extends BasicController{
    @Autowired
    TenantMapper tenantMapper;

    /**** 推送页面  *****/
    @RequestMapping("/sys/tenant")
    public String listTenant(Model m, HttpSession session) {
        initModel(m, session);

        return "/page/tenant_cfg";
    }

    @RequestMapping("/sys/tenant/add")
    public String addTenant(Model m, HttpSession session) {
        initModel(m, session);
        return "/page/table/tenant_add";
    }

    @RequestMapping("/sys/tenant/edit")
    public String editTenant(Model m, HttpSession session) {
        initModel(m, session);
        return "/page/table/tenant_add";
    }

    /**** 推送数据  *****/
    @RequestMapping("/sys/tenant/list")
    @ResponseBody
    public JSONObject list(HttpSession session, int page, int limit, String searchParams) {
        Page<Tenant> pageable = new Page<>(page, limit);
        QueryWrapper<Tenant> queryWrapper = new QueryWrapper<>();

        IPage<Tenant> cs = tenantMapper.selectPage(pageable, queryWrapper);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("msg", "");
        jsonObject.put("count", cs.getTotal());
        jsonObject.put("data", cs.getRecords());
        return jsonObject;
    }

    @PostMapping("/sys/tenant/update")
    @ResponseBody
    public String update(HttpSession session, @RequestBody JSONObject object) throws Exception {
        // 从请求参数中创建对象
        Tenant tenantNew = JSONObject.toJavaObject(object,Tenant.class);

        // 检测该对象是否已存在
        Tenant tenantOld = tenantMapper.selectById(tenantNew.getId());
        //新增或更新
        int num = 0;
        LocalDateTime now = LocalDateTime.now();
        if(null != tenantOld) {
            tenantNew.setCreate_time(tenantOld.getCreate_time());
            tenantNew.setUpdate_time(now);
            num = tenantMapper.updateById(tenantNew);
        }
        else{
            QueryWrapper<Tenant> queryWrapper = new QueryWrapper<>();
            queryWrapper.orderByDesc("id");
            queryWrapper.last("limit 1");
            Tenant maxTenant = tenantMapper.selectOne(queryWrapper);
            tenantNew.setId(maxTenant.getId()+1);
            tenantNew.setCreate_time(now);
            tenantNew.setUpdate_time(now);
            num = tenantMapper.insert(tenantNew); //id自增
        }
        if (num == 0) {
            return HttpResult.failed();
        } else {
            return HttpResult.ok();
        }
    }

    @PostMapping("/sys/tenant/delete")
    @ResponseBody
    public String delete(@RequestParam String name) throws Exception {
        QueryWrapper<Tenant> wrapper = new QueryWrapper<>();
        wrapper.eq("name",name);
        int num = tenantMapper.delete(wrapper);
        if (num == 0) {
            return HttpResult.failed();
        } else return HttpResult.ok();
    }

}
