package com.ruoyi.tc.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.tc.entity.ExamineInfo;
import com.ruoyi.tc.entity.Unit;
import com.ruoyi.tc.entity.UnitOtherConcat;
import com.ruoyi.tc.service.ExamineInfoService;
import com.ruoyi.tc.service.UnitOtherConcatService;
import com.ruoyi.tc.service.UnitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 组织架构
 *
 * @author du
 * @since 2024/12/9 10:42
 */
@Api(tags = "组织架构")
@RestController
@RequestMapping("/tc/schema")
public class DeptSchemaController {

    @Resource
    private UnitService unitService;

    @Resource
    private UnitOtherConcatService unitOtherConcatService;

    @Resource
    private ExamineInfoService examineInfoService;
    /**
     * 根据资产类型返回组织架构
     */
    @ApiOperation(value = "根据资产类型返回组织架构")
    @GetMapping
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "0:web资产，1:小程序资产，2：公众号资产，3：电子邮件资产，4：移动应用程序资产", required = true),
    })
    public AjaxResult schema(@RequestParam("type") String type) {
        return AjaxResult.success(unitService.getSchema(type));
    }

    /**
     * 单位查看自己单位具体信息
     */
    @ApiOperation(value = "单位查看自己单位具体信息")
    @PreAuthorize("@ss.hasAnyRoles('unit')")
    @GetMapping("/unitOwnInfo")
    public AjaxResult unitOwnInfo() {
        Unit one = unitService.lambdaQuery().eq(Unit::getUserName, SecurityUtils.getLoginUser().getUsername()).one();
        Unit byId = unitService.getById(one.getId());
        return AjaxResult.success(byId);
    }

    /**
     * 单位修改自己的单位信息
     */
    @ApiOperation(value = "单位修改自己的单位信息")
    @PreAuthorize("@ss.hasAnyRoles('unit')")
    @PostMapping("/unitEditOwn")
    public AjaxResult unitEditOwn(@RequestBody Unit x) {
        Unit byId = unitService.getById(x.getId());
        if (!byId.getUserName().equals(x.getUserName()) || !byId.getNickName().equals(x.getNickName())) {
            throw new ServiceException("单位名称和社会信用代码不允许修改!");
        }
        unitService.updateById(x);
        //先删除
        unitOtherConcatService.deleteByUnitIds(x.getId());
        examineInfoService.deleteByUnitIds(x.getId());

        if (x.getOtherConcat() != null) {
            x.getOtherConcat().forEach(y -> {
                y.setUnitId(x.getId());
                y.setConcatId(null);
            });
            unitOtherConcatService.saveBatch(x.getOtherConcat());
        }
        if (x.getJcxxList() != null) {
            x.getJcxxList().forEach(y -> {
                y.setUnitId(x.getId());
                y.setJcid(null);
            });
            examineInfoService.saveBatch(x.getJcxxList());
        }
        return AjaxResult.success();
    }

}
