/*
 * Author: wangjian
 * 生成controller
 */
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;

/**
 * 系统通用码表子级表
 *
 * @author wangjieliu
 * @date Thu Mar 18 00:00:00 CST 2021
 */
@RestController
@AllArgsConstructor
@RequestMapping("/syscommoncode")
@Api(tags = "系统通用码表接口")
public class SyschildcodeController {

    private final SyschildcodeService syschildcodeService;

    /**
     * 新增系统通用码表子级表
     *
     * @param syschildcode 系统通用码表子级表
     * @return ResultRespone
     */
    @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());
    }

    /**
     * 分页查询
     *
     * @return
     */
    @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)));
    }

    /**
     * 通过id查询系统通用码表子级表
     *
     * @param ccid id
     * @return ResultRespone
     */
    @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));
    }


    /**
     * 修改系统通用码表子级表
     *
     * @param syschildcode 系统通用码表子级表
     * @return ResultRespone
     */
    @ApiOperation(value = "修改系统通用码表")
    @PutMapping
    public ResultRespone updateById(@RequestBody Syschildcode syschildcode) {
        //修改前查询是否系统编码，如果是系统编码，则无法修改
        String flag = syschildcodeService.getOne(Wrappers.<Syschildcode>lambdaQuery().eq(Syschildcode::getId, syschildcode.getId()).select(Syschildcode::getFlag)).getFlag();
        if ("1".equals(flag)) {
            throw new ToflyDeniedException("该编码是系统编码，无法修改");
        }
       /* //由于编码表特殊；修改删除会影响系统的进行；所以目前只能修改厂家的编码
        Long cjId = 2L;
        if (!cjId.equals(syschildcode.getPcid())) {
            throw new ToflyDeniedException("修改失败");
        }*/
        if (StringUtil.isNotNull(syschildcode.getPcid())) {
            Syschildcode code = syschildcodeService.getOne(Wrappers.<Syschildcode>lambdaQuery().eq(Syschildcode::getCname, syschildcode.getCname())
                    .eq(Syschildcode::getPcid, syschildcode.getPcid()).ne(Syschildcode::getId , syschildcode.getId()));
            if (StringUtil.isNotNull(code)) {
                throw new ToflyDeniedException("存在相同名字，请勿重复添加");
            }
            Syschildcode ccode = syschildcodeService.getOne(Wrappers.<Syschildcode>lambdaQuery().eq(Syschildcode::getCcode, syschildcode.getCcode())
                    .eq(Syschildcode::getPcid, syschildcode.getPcid()).ne(Syschildcode::getId , syschildcode.getId()));
            if (StringUtil.isNotNull(ccode)) {
                throw new ToflyDeniedException("编码重复");
            }
        } else {
            Syschildcode code = syschildcodeService.getOne(Wrappers.<Syschildcode>lambdaQuery().eq(Syschildcode::getCname, syschildcode.getCname())
                    .isNull(Syschildcode::getPcid).ne(Syschildcode::getId , syschildcode.getId()));
            if (StringUtil.isNotNull(code)) {
                throw new ToflyDeniedException("存在相同名字，请勿重复添加");
            }
            Syschildcode ccode = syschildcodeService.getOne(Wrappers.<Syschildcode>lambdaQuery().eq(Syschildcode::getCcode, syschildcode.getCcode())
                    .isNull(Syschildcode::getPcid).ne(Syschildcode::getId , syschildcode.getId()));
            if (StringUtil.isNotNull(ccode)) {
                throw new ToflyDeniedException("编码重复");
            }
        }
        return ResultRespone.success(syschildcodeService.updateById(syschildcode));
    }

    /**
     * 删除编码表
     * 由于编码表的特殊性；怕删除后对系统有所影响；所以采用了字段软删除
     *
     * @param ids
     * @return
     */
    @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")));
    }


}
