package com.zcxk.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcxk.entity.GisSetting;
import com.zcxk.entity.SystemSetting;
import com.zcxk.model.*;
import com.zcxk.service.GisSettingService;
import com.zcxk.service.SystemSettingService;
import com.zcxk.util.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统设置控制层
 *
 * @author yjy
 * @since 2020-11-16
 */
@RestController
@RequestMapping("/systemSetting")
@Api(tags = "系统设置")
public class SystemSettingController {
    /**
     * 服务对象
     */
    @Autowired
    private SystemSettingService systemSettingService;
    @Autowired
    private GisSettingService gisSettingService;

    /**
     * 根据机构查询系统设置数据
     *
     * @param companyOrgId 所属公司
     * @param deptOrgId 所属部门
     * @return 单条数据
     */
    @RequestMapping(value = "selectSystem", method = RequestMethod.GET)
    @ApiOperation(value = "根据机构查询单条系统配置数据")
    public AjaxMessage<SystemSetting> selectSystem(
            @ApiParam(value = "所属公司", required = false) @RequestParam(required = false) Integer companyOrgId,
            @ApiParam(value = "所属部门", required = false) @RequestParam(required = false) Integer deptOrgId) {
        LoginUser currentUser = UserUtil.getCurrentUser();
       if(companyOrgId==null&&deptOrgId==null) {
           companyOrgId=currentUser.getCompanyId();
           deptOrgId=currentUser.getDepartmentId();
       }
        SystemSetting result = systemSettingService.selectSystem(companyOrgId,deptOrgId);
        return new AjaxMessage<>(ResultStatus.OK, result);
    }
    /**
     * 保存系统配置数据
     *
     * @param systemSetting 系统设置信息
     * @return
     */
    @RequestMapping(value = "saveSystem", method = RequestMethod.POST)
    @ApiOperation(value = "保存单条系统配置数据")
    public AjaxMessage<Boolean> saveSystem(
            @ApiParam(value = "系统设置信息", required = true) @RequestBody SystemSetting systemSetting) {
        LoginUser currentUser = UserUtil.getCurrentUser();
        Integer id = systemSetting.getId() != null?systemSetting.getId():systemSettingService.getId(systemSetting.getCompanyOrgId(),systemSetting.getDeptOrgId());
        if(id==null){//没有查询到数据则新增
            systemSetting.setCreateBy(currentUser.getUsername());
        }else {//修改
            systemSetting.setId(id);
            systemSetting.setUpdateBy(currentUser.getUsername());
        }
        systemSetting.setTenantId(currentUser.getTenantId());
        systemSetting.setStatus(1);
        boolean result = systemSettingService.saveSystem(systemSetting);

        return new AjaxMessage<>(ResultStatus.OK, result);
    }
    /**
     * 根据机构查询Gis设置数据
     *
     * @param companyOrgId 所属公司
     * @param deptOrgId 所属部门
     * @return
     */
    @RequestMapping(value = "selectGis", method = RequestMethod.GET)
    @ApiOperation(value = "查询Gis配置数据")
    public AjaxMessage<List<GisSetting>> selectGis(
            @ApiParam(value = "所属公司", required = false) @RequestParam(required = false) Integer companyOrgId,
            @ApiParam(value = "所属部门", required = false) @RequestParam(required = false) Integer deptOrgId,
            @ApiParam(value = "服务类型", required = false) @RequestParam(required = false) String serviceType) {
        LoginUser currentUser = UserUtil.getCurrentUser();
        //如果机构id不传，默认查询当前角色下所属机构
        if(companyOrgId==null&&deptOrgId==null) {
            companyOrgId=currentUser.getCompanyId();
            deptOrgId=currentUser.getDepartmentId();
        }

        List<GisSetting> result = gisSettingService.selectGis(companyOrgId,deptOrgId,serviceType);

        return new AjaxMessage<>(ResultStatus.OK, result);
    }
    /**
     * 根据机构分页查询Gis设置数据
     *
     * @param companyOrgId 所属公司
     * @param deptOrgId 所属部门
     * @return
     */
    @RequestMapping(value = "selectPageGis", method = RequestMethod.GET)
    @ApiOperation(value = "根据机构查询Gis配置数据")
    public AjaxMessage<Pagination<GisSetting>> selectPageGis(
            @ApiParam(value = "页数", required = true)@RequestParam Integer pageNum,
            @ApiParam(value = "条数", required = true)@RequestParam Integer pageSize,
            @ApiParam(value = "所属公司", required = false) @RequestParam(required = false) Integer companyOrgId,
            @ApiParam(value = "所属部门", required = false) @RequestParam(required = false) Integer deptOrgId,
            @ApiParam(value = "服务类型", required = false) @RequestParam(required = false) String serviceType) {
        if(companyOrgId==null&&deptOrgId==null) {
            return new AjaxMessage<>(ResultStatus.PARAM_ERROR);
        }

        IPage<GisSetting> iPage = new Page<>(pageNum, pageSize);
        iPage = gisSettingService.selectPageGis(iPage, companyOrgId,deptOrgId,serviceType);
        Pagination<GisSetting> pages = new Pagination<>(iPage);
        return new AjaxMessage<>(ResultStatus.OK, pages);
    }
    /**
     * 新增系统配置数据
     *
     * @param listGisSetting Gis设置信息
     * @return
     */
    @RequestMapping(value = "saveGis", method = RequestMethod.POST)
    @ApiOperation(value = "保存多条GIS配置数据")
    public AjaxMessage<Boolean> saveGis(@ApiParam(value = "系统设置信息", required = true) @RequestBody List<GisSetting> listGisSetting) {
        LoginUser currentUser = UserUtil.getCurrentUser();
        for (GisSetting gis:listGisSetting) {
            Integer id = gis.getId() != null?gis.getId():gisSettingService.getId(gis.getCompanyOrgId(),gis.getDeptOrgId(),gis.getServiceType());
            if(id==null){//没有查询到数据则新增
                gis.setCreateBy(currentUser.getUsername());
            }else{//修改
                gis.setId(id);
                gis.setUpdateBy(currentUser.getUsername());
            }
            gis.setTenantId(currentUser.getTenantId());
            gis.setStatus(1);
        }
        boolean result = gisSettingService.saveGis(listGisSetting);

        return new AjaxMessage<>(ResultStatus.OK, result);
    }
    /**
     * 删除多条数据
     *
     * @param ids
     * @return Response对象
     */
    @RequestMapping(value = "deleteByGisIds", method = RequestMethod.POST)
    @ApiOperation(value = "删除Gis配置信息")
    public AjaxMessage<Boolean> deleteByGisIds(@ApiParam(value = "gis配置ids", required = true) @RequestBody Long[] ids) {
        boolean result = gisSettingService.deleteByGisIds(ids);
        return new AjaxMessage<>(ResultStatus.OK, result);
    }

}