package com.zcxk.charge.web.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcxk.charge.api.dto.query.GisPageDto;
import com.zcxk.charge.api.enums.ChargeErrorEnum;
import com.zcxk.charge.web.common.Pagination;
import com.zcxk.charge.core.entity.GisSetting;
import com.zcxk.charge.core.entity.SystemSetting;
import com.zcxk.charge.web.service.GisSettingService;
import com.zcxk.charge.web.service.SystemSettingService;
import com.zcxk.core.common.pojo.AjaxMessage;
import com.zcxk.core.oauth2.pojo.LoginUser;
import com.zcxk.core.oauth2.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 javax.validation.Valid;
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;

    @RequestMapping(value = "selectSystem", method = RequestMethod.GET)
    @ApiOperation(value = "根据机构查询单条系统配置数据")
    public AjaxMessage<SystemSetting> selectSystem(
            @ApiParam(value = "所属公司") @RequestParam(required = false) Integer companyOrgId,
            @ApiParam(value = "所属部门") @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 AjaxMessage.success( result);
    }

    @RequestMapping(value = "saveSystem", method = RequestMethod.POST)
    @ApiOperation(value = "保存单条系统配置数据")
    public AjaxMessage 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);
        return AjaxMessage.success( systemSettingService.saveSystem(systemSetting));
    }

    @RequestMapping(value = "selectGis", method = RequestMethod.POST)
    @ApiOperation(value = "查询Gis配置数据")
    public AjaxMessage selectGis(@RequestBody @Valid GisPageDto dto) {
        LoginUser currentUser = UserUtil.getCurrentUser();
        // 如果机构id不传，默认查询当前角色下所属机构
        if (dto.getDeptOrgId()==null&&dto.getCompanyOrgId()==null) {
            dto.setCompanyOrgId(currentUser.getCompanyId());
            dto.setDeptOrgId(currentUser.getDepartmentId());
        }
        return AjaxMessage.success(gisSettingService.selectGis(dto.getCompanyOrgId(), dto.getDeptOrgId(), dto.getServiceType()));
    }

    @RequestMapping(value = "selectPageGis", method = RequestMethod.POST)
    @ApiOperation(value = "根据机构查询Gis配置数据")
    public AjaxMessage selectPageGis(@RequestBody @Valid GisPageDto dto) {
        if (dto.getCompanyOrgId() == null && dto.getDeptOrgId() == null) {
            return AjaxMessage.fail(ChargeErrorEnum.PARAM_ERROR);
        }
        IPage<GisSetting> iPage = new Page<>(dto.getPageNum(), dto.getPageSize());
        iPage = gisSettingService.selectPageGis(iPage, dto.getCompanyOrgId(), dto.getDeptOrgId(), dto.getServiceType());
        Pagination<GisSetting> pages = new Pagination<>(iPage);
        return AjaxMessage.success( pages);
    }

    @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);
        }
        return AjaxMessage.success(gisSettingService.saveGis(listGisSetting));
    }

    @RequestMapping(value = "deleteByGisIds", method = RequestMethod.POST)
    @ApiOperation(value = "删除Gis配置信息")
    public AjaxMessage<Boolean> deleteByGisIds(@ApiParam(value = "gis配置ids", required = true) @RequestBody Long[] ids) {
        return AjaxMessage.success(gisSettingService.deleteByGisIds(ids));
    }

}