package com.tofly.base.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.base.entity.Syschildcode;
import com.tofly.base.service.SyschildcodeService;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.core.util.StringUtil;
import com.tofly.common.oauth.exception.ToflyDeniedException;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@AllArgsConstructor
@RequestMapping("/syscommoncode")
@Api(tags = "系统通用码表接口")
public class SyschildcodeController {

    private final SyschildcodeService syschildcodeService;

    @ApiOperation(value = "新增系统通用码表")
    @PostMapping
    public ResultRespone save(@RequestBody Syschildcode syschildcode) {
        if (StringUtil.isEmpty(syschildcode.getFlag())) {
            throw new ToflyDeniedException("请输入编码标识");
        }
        return ResultRespone.success(syschildcodeService.insert(syschildcode));
    }

    @ApiModelProperty(value = "通过ID得到编码名称")
    @GetMapping("/getname")
    @ResponseBody
    public String getname(Long id) {
        return syschildcodeService.getOne(Wrappers.<Syschildcode>lambdaQuery().eq(Syschildcode::getId, id).select(Syschildcode::getCname)).getCname();
    }

    @ApiOperation(value = "分页获取编码")
    @GetMapping("/getByObj")
    public IPage<Syschildcode> getByObj(Syschildcode syschildcode, Page page) {
        String cname = syschildcode.getCname();
        String pcname = syschildcode.getPcname();
        Long pid = syschildcode.getPcid();
         Page<Syschildcode> list= syschildcodeService.page(page, Wrappers.<Syschildcode>lambdaQuery()
                .eq(Syschildcode::getCdisable, "1")
                .like(StringUtil.isNotEmpty(cname), Syschildcode::getCname, cname)
                .eq(StringUtil.isNotEmpty(pcname), Syschildcode::getPcname, pcname)
                .eq(StringUtil.isNotNull(pid), Syschildcode::getPcid, pid));
         return list;
    }

    @ApiOperation(value = "分页获取所有编码")
    @GetMapping("/page")
    public ResultRespone getDataPage(Page page,Syschildcode syschildcode) {

       return ResultRespone.success(syschildcodeService.getDataPage(page,syschildcode));
    }

    @ApiOperation(value = "获取父级和子级所有目录")
    @GetMapping("/all")
    public ResultRespone get() {
        return ResultRespone.success(syschildcodeService.getAll());
    }

    @GetMapping("/fj")
    @ApiOperation(value = "获取所有的父级节点，分页")
    public ResultRespone getSysParentPage(Page page,Syschildcode syschildcode) {

        return ResultRespone.success(syschildcodeService.getParentList(page,syschildcode));
    }


    @ApiOperation(value = "根据父级ID获取所有子节点,树形")
    @GetMapping("/getByfjId")
    public ResultRespone getConfig(String pCid) {
        List<String> list = Arrays.asList(pCid.split(","));
        return ResultRespone.success(syschildcodeService.getBylistPcid(list));
    }


    @ApiOperation(value = "根据条件获取子级编码信息")
    @PostMapping("/getChild")
    public ResultRespone getChild(@RequestBody Syschildcode code) {
        return ResultRespone.success(syschildcodeService.list(Wrappers.query(code)));
    }

    @GetMapping("/{ccid}")
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ccid", value = "ID", required = true, dataType = "Long")
    })
    public ResultRespone getById(@PathVariable("ccid") Long ccid) {
        return ResultRespone.success(syschildcodeService.getById(ccid));
    }

    @ApiOperation(value = "修改系统通用码表")
    @PutMapping
    public ResultRespone updateById(@RequestBody Syschildcode syschildcode) {
        return ResultRespone.success(syschildcodeService.updateInfo(syschildcode));
    }

    @ApiOperation(value = "删除系统编码表")
    @DeleteMapping("/{ids}")
    public ResultRespone deleteByIds(@PathVariable("ids") String ids) {
        if (StringUtil.isEmpty(ids)) {
            throw new ToflyDeniedException("请传入编码id");
        }
        List<String> idList = Arrays.asList(ids.split(","));
      Integer count= syschildcodeService.count(
                Wrappers.<Syschildcode>lambdaQuery().
                        eq(Syschildcode::getFlag,"1")
                        .in(Syschildcode::getId,idList));

      if(count>0){
          throw new ToflyDeniedException("存在系统编码，无法删除");
      }
        return ResultRespone.success(syschildcodeService.update(Wrappers.<Syschildcode>lambdaUpdate()
                .in(Syschildcode::getId, idList).set(Syschildcode::getCdisable, "0")));
    }

    @ApiOperation(value = "修改码表名称")
    @PutMapping("/updateNameById")
    public ResultRespone updateNameById(@RequestBody Syschildcode syschildcode) {
        Long id = syschildcode.getId();
        String name = syschildcode.getCname();
        return ResultRespone.success(syschildcodeService.update(Wrappers.<Syschildcode>lambdaUpdate()
                .eq(Syschildcode::getId , id).set(Syschildcode::getCname , name)));
    }
}
