package com.qmth.themis.backend.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qmth.themis.business.annotation.ApiJsonObject;
import com.qmth.themis.business.annotation.ApiJsonProperty;
import com.qmth.themis.business.config.SystemConfig;
import com.qmth.themis.business.constant.SystemConstant;
import com.qmth.themis.business.dto.response.TBOrgDto;
import com.qmth.themis.business.entity.TBOrg;
import com.qmth.themis.business.entity.TBUser;
import com.qmth.themis.business.service.TBOrgService;
import com.qmth.themis.business.util.EhcacheUtil;
import com.qmth.themis.business.util.RedisUtil;
import com.qmth.themis.business.util.ServletUtil;
import com.qmth.themis.common.contanst.Constants;
import com.qmth.themis.common.enums.ExceptionResultEnum;
import com.qmth.themis.common.exception.BusinessException;
import com.qmth.themis.common.util.Result;
import com.qmth.themis.common.util.ResultUtil;
import io.swagger.annotations.*;
import org.springframework.cache.annotation.CachePut;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;

/**
 * @Description: 机构 前端控制器
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2020/6/25
 */
@Api(tags = "机构Controller")
@RestController
@RequestMapping("/${prefix.url.admin}/org")
public class TBOrgController {

    @Resource
    TBOrgService tbOrgService;

    @Resource
    RedisUtil redisUtil;

//    @CachePut(value = "org_cache", key = "'orgCacheQuery'")
//    @Cacheable(value = "org_cache", key = "#p0")
    @ApiOperation(value = "机构查询分页接口")
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "机构信息", response = TBOrgDto.class)})
    public Result queryByPage(@ApiParam(value = "机构代码", required = false) @RequestParam(required = false) String code, @ApiParam(value = "机构名称", required = false) @RequestParam(required = false) String name, @ApiParam(value = "是否启用", required = false) @RequestParam(required = false) Integer enable, @ApiParam(value = "分页页码", required = true) @RequestParam int pageNumber, @ApiParam(value = "分页数", required = true) @RequestParam int pageSize) {
        return ResultUtil.ok(tbOrgService.queryByPage(new Page<>(pageNumber, pageSize), code, name, enable));
    }

    //    @CacheEvict(value = "org_cache", key = "'orgCacheQuery'")
    @ApiOperation(value = "机构新增/编辑接口")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @Transactional
    @ApiResponses({@ApiResponse(code = 200, message = "{\"success\":true}", response = Result.class)})
    public Result save(@ApiParam(value = "机构信息", required = true) @RequestBody TBOrg tbOrg) {
        if (Objects.isNull(tbOrg)) {
            throw new BusinessException(ExceptionResultEnum.ORG_INFO_IS_NULL);
        }
        TBUser tbUser = (TBUser) ServletUtil.getRequestAccount();
        if (Objects.isNull(tbOrg.getId())) {
            tbOrg.setId(Constants.idGen.next());
            tbOrg.setCreateId(tbUser.getId());
        } else {
            tbOrg.setUpdateId(tbUser.getId());
        }
//        if (Objects.nonNull(tbOrg.getLogo()) && !Objects.equals(tbOrg.getLogo(), "")) {
//            tbOrg.setLogo(tbOrg.getLogo().replaceAll(systemConfig.getProperty("aliyun.oss.url") + File.separator, ""));
//        }
        redisUtil.setOrg(tbOrg.getId(), tbOrg);
        EhcacheUtil.put(SystemConstant.orgCodeCache, tbOrg.getCode(), tbOrg);
        tbOrgService.saveOrUpdate(tbOrg);
        return ResultUtil.ok(Collections.singletonMap("success", true));
    }

    @ApiOperation(value = "机构停用/启用接口")
    @RequestMapping(value = "/enable", method = RequestMethod.POST)
    @Transactional
    @ApiResponses({@ApiResponse(code = 200, message = "{\"success\":true}", response = Result.class)})
    public Result enableOrg(@ApiJsonObject(name = "enableOrg", value = {
            @ApiJsonProperty(key = "id", type = "long", example = "1", description = "机构id"),
            @ApiJsonProperty(key = "enable", type = "int", example = "1", description = "停用/启用")
    }) @ApiParam(value = "用户信息", required = true) @RequestBody Map<String, Object> user) {
        if (Objects.isNull(user.get("id")) || Objects.equals(user.get("id"), "")) {
            throw new BusinessException(ExceptionResultEnum.ORG_ID_IS_NULL);
        }
        Long orgId = Long.parseLong(String.valueOf(user.get("id")));
        if (Objects.isNull(user.get("enable")) || Objects.equals(user.get("enable"), "")) {
            throw new BusinessException(ExceptionResultEnum.ENABLE_IS_NULL);
        }
        Integer enable = Integer.parseInt(String.valueOf(user.get("enable")));
        TBOrg tbOrg = (TBOrg) redisUtil.getOrg(orgId);
        if (Objects.isNull(tbOrg)) {
            tbOrg = tbOrgService.getById(orgId);
        }
        if (Objects.isNull(tbOrg)) {
            throw new BusinessException(ExceptionResultEnum.USER_NO);
        }
        //保存机构
        tbOrg.setEnable(enable);
        tbOrgService.updateById(tbOrg);
        redisUtil.setOrg(orgId, tbOrg);
        EhcacheUtil.put(SystemConstant.orgCodeCache, tbOrg.getCode(), tbOrg);
        return ResultUtil.ok(Collections.singletonMap("success", true));
    }
}