package com.zxdmy.excite.admin.controller.cloud.account;

import com.zxdmy.excite.common.base.BaseController;
import com.zxdmy.excite.common.base.BaseResult;
import com.zxdmy.excite.common.entity.PageList;
import com.zxdmy.excite.framework.aop.AnnotationSaveReLog;
import com.zxdmy.excite.system.entity.cloud.account.CloudCompany;
import com.zxdmy.excite.system.entity.req.account.ReqCompany;
import com.zxdmy.excite.system.entity.vo.account.CompanyVo;
import com.zxdmy.excite.system.service.cloud.account.ICloudCompanyService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@AllArgsConstructor
@RequestMapping("/cloud/account/company")
public class AccountEnterpriseController extends BaseController {

    private ICloudCompanyService iCloudCompanyService;


    @RequestMapping("index")
    public String index() {
        return "cloud/account/enterprise/index";
    }


    @GetMapping("/list")
    @ResponseBody
    public BaseResult getPageList(Integer page, Integer limit, ReqCompany data) {
        try {
            PageList<CompanyVo> dataPage = iCloudCompanyService.queryByPage(page, limit, data);
            return success("获取数据成功", dataPage.getList(), (int) dataPage.getTotal());
        } catch (Exception e) {
            return error(500, "发生错误:" + e.getMessage());
        }
    }


    @RequestMapping("add")
    public String add() {
        return "cloud/account/enterprise/add";
    }

    @PostMapping("/addSave")
    @ResponseBody
    @AnnotationSaveReLog
    public BaseResult addSave(@Validated ReqCompany data) {
        try {
            if(null == iCloudCompanyService.getByData(data)){
                if(iCloudCompanyService.saveData(data)==1){
                    return success("操作成功");
                }else{
                    return error(400, "操作失败，系统错误");
                }
            }else{
                return error(400, "操作失败，数据已存在");
            }
        } catch (Exception e) {
            return error(500, "发生错误:" + e.getMessage());
        }
    }

    @RequestMapping("edit/{id}")
    public String edit(@PathVariable String id, ModelMap map) {
        CloudCompany data = iCloudCompanyService.getById(id);
        // 加载至前端
        if (data == null) {
            map.put("code", 404);
            map.put("msg", "数据不存在！");
            return "error/error";
        }
        map.put("id", id);
        return "cloud/account/enterprise/edit";
    }

    @GetMapping("/getData")
    @ResponseBody
    public BaseResult getData(String id) {
        try {
            CloudCompany data = iCloudCompanyService.getById(id);
            if(null!=data){
                return success("获取数据成功",data);
            }else{
                return error(400, "数据不存在！");
            }


        } catch (Exception e) {
            return error(500, "发生错误:" + e.getMessage());
        }
    }

    @PostMapping("/editSave")
    @ResponseBody
    @AnnotationSaveReLog
    public BaseResult editSave(@Validated ReqCompany data) {
        try {
            if(null == iCloudCompanyService.getByData(data)){
                if(iCloudCompanyService.updateData(data)==1){
                    return success(200,"操作成功");
                }else{
                    return error(400, "操作失败，系统错误");
                }
            }else{
                return error(400, "操作失败，数据已存在");
            }
        } catch (Exception e) {
            return error(500, "发生错误:" + e.getMessage());
        }
    }

    @PostMapping("/delete")
    @ResponseBody
    @AnnotationSaveReLog
    public BaseResult delete(String id) {
        try {
            int result = iCloudCompanyService.deleteById(id);
            if (result == 1) {
                return success(200, "删除成功！");
            }else{
                return error("删除失败！");
            }
        } catch (Exception e) {
            return error("发生错误:" + e.getMessage());
        }
    }

    @GetMapping("/query")
    @ResponseBody
    public BaseResult query() {
        try {
            List<CompanyVo> dataPage = iCloudCompanyService.queryByData();
            return success("获取数据成功", dataPage);
        } catch (Exception e) {
            return error(500, "发生错误:" + e.getMessage());
        }
    }
}
