/*
 * Author: wangjian
 * 生成controller
 */
package com.tofly.xrtymis.custom.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.core.util.StringUtil;
import com.tofly.common.log.annotation.ToFlyAppLog;
import com.tofly.xrtymis.custom.dto.other.GdConfigDto;
import com.tofly.xrtymis.custom.entity.OrderTypeByIdResult;
import com.tofly.xrtymis.custom.service.GddepartmentService;
import com.tofly.xrtymis.custom.service.GdflowconfigService;
import com.tofly.xrtymis.custom.service.GdpersonService;
import com.tofly.xrtymis.entity.Gddepartment;
import com.tofly.xrtymis.entity.Gdflowconfig;
import com.tofly.xrtymis.entity.Gdperson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

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

/**
 * ${comments}
 *
 * @author fcl
 * @date Mon May 31 00:00:00 CST 2021
 */
@RestController
@AllArgsConstructor
@RequestMapping("/gdflowconfig")
@Api(tags = "工单及其部门配置接口")
public class GdflowconfigController {

    private final GdflowconfigService gdflowconfigService;
    private final GddepartmentService gddepartmentService;
    private final GdpersonService gdpersonService;

    /**
     * 分页查询
     * sfDelete 是否删除 0 删除,1保留
     *
     * @param page         分页对象
     * @param gdflowconfig
     * @return
     */
//    @GetMapping("/page")
//    @ApiOperation(value = "分页查询")
//    public ResultRespone getGdflowconfigPage(Page page, @ModelAttribute Gdflowconfig gdflowconfig) {
//        return ResultRespone.success(gdflowconfigService.page(page, Wrappers.query(gdflowconfig)));
//    }

    /**
     * 通过id查询${comments}
     *
     * @param id id
     * @return ResultRespone
     */
    @GetMapping("/getById")
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "node", value = "流程节点", required = false, dataType = "String")
    })
    public ResultRespone getById(Long id, String node) {
        Gdflowconfig gdflowconfig = gdflowconfigService.getById(id);
        if (gdflowconfig != null) {
            Long configId = gdflowconfig.getId();
            //部门信息集合
            List<Gddepartment> gddepartmentList = gddepartmentService.list(Wrappers.<Gddepartment>lambdaQuery().eq(Gddepartment::getConfigId , configId)
                    .select(Gddepartment::getDepartId , Gddepartment::getDepartName));
            //人员信息集合
            List<Gdperson> gdpersonList = gdpersonService.list(Wrappers.<Gdperson>lambdaQuery().eq(Gdperson::getConfigId , configId)
                    .eq(StringUtil.isNotEmpty(node), Gdperson::getNode , node).orderByAsc(Gdperson::getChildNode));
            OrderTypeByIdResult orderTypeByIdResult = new OrderTypeByIdResult();
            BeanUtils.copyProperties(gdflowconfig , orderTypeByIdResult);
            orderTypeByIdResult.setGddepartmentList(gddepartmentList);
            orderTypeByIdResult.setGdpersonList(gdpersonList);
            return ResultRespone.success(orderTypeByIdResult);
        }else {
            return ResultRespone.failed(false , "暂无结果");
        }
    }

    /**
     * 通过工单类型查询,获取未删除工单类型数据列表
     */
    @GetMapping("/selectBygdlx")
    @ApiOperation(value = "通过工单类型查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类型ID", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "xf1id", value = "细分1ID", required = false, dataType = "Long"),
            @ApiImplicitParam(name = "xf2id", value = "细分2ID", required = false, dataType = "Long"),
            @ApiImplicitParam(name = "node", value = "流程节点", required = false, dataType = "String")
    })
    public ResultRespone selectBygdlx(Long id, Long xf1id, Long xf2id, String node) {
        if (id == null) {
            return ResultRespone.failed(null, "工单类型id 不能为空！");
        }
        if (xf2id != null && xf1id == null) {
            return ResultRespone.failed(null, "细分2ID不为空，细分1ID不能为空！");
        }
        Gdflowconfig gdflowconfig = gdflowconfigService.loadGdFlowConfig(id, xf1id, xf2id);
        if (gdflowconfig != null) {
            Long configId = gdflowconfig.getId();
            //部门信息集合
            List<Gddepartment> gddepartmentList = gddepartmentService.list(Wrappers.<Gddepartment>lambdaQuery().eq(Gddepartment::getConfigId , configId)
                    .select(Gddepartment::getDepartId , Gddepartment::getDepartName));
            //人员信息集合
            List<Gdperson> gdpersonList = gdpersonService.list(Wrappers.<Gdperson>lambdaQuery().eq(Gdperson::getConfigId , configId)
                    .eq(StringUtil.isNotEmpty(node), Gdperson::getNode , node).orderByAsc(Gdperson::getChildNode));
            OrderTypeByIdResult orderTypeByIdResult = new OrderTypeByIdResult();
            BeanUtils.copyProperties(gdflowconfig , orderTypeByIdResult);
            orderTypeByIdResult.setGddepartmentList(gddepartmentList);
            orderTypeByIdResult.setGdpersonList(gdpersonList);

            return ResultRespone.success(orderTypeByIdResult);
        }
        return ResultRespone.failed(null, "工单类型、细分1、细分1不存在流程！");
    }


    /**
     * 新增
     *
     * @param gdflowconfig
     * @return ResultRespone
     */
    @ToFlyAppLog(title = "新增")
    @ApiOperation(value = "新增")
    @PostMapping
    public ResultRespone save(@RequestBody Gdflowconfig gdflowconfig) {
        if (gdflowconfig.getTransferOutType() != null && gdflowconfig.getTabType() != null) {
            return ResultRespone.failed(false, "转入其它流程与接入其它流程不能同时存在");
        }
        Gdflowconfig selGdf = new Gdflowconfig();
        selGdf.setGdlx(gdflowconfig.getGdlx());
        selGdf.setGdxf1(gdflowconfig.getGdxf1());
        selGdf.setGdxf2(gdflowconfig.getGdxf2());
        selGdf.setSfDelete("1");
        gdflowconfig.setTabTypeName(null);
        gdflowconfig.setTransferOutTypeName(null);
        int count = gdflowconfigService.count(Wrappers.query(selGdf).lambda().isNull(gdflowconfig.getGdxf1() == null, Gdflowconfig::getGdxf1).isNull(gdflowconfig.getGdxf2() == null, Gdflowconfig::getGdxf2));
        if (count > 0) {
            return ResultRespone.failed(false, "配置类型重复");
        }
        return ResultRespone.success(gdflowconfigService.save(gdflowconfig));
    }

    /**
     * 修改${comments}
     *
     * @param gdflowconfig ${comments}
     * @return ResultRespone
     */
    @ToFlyAppLog(title = "修改")
    @ApiOperation(value = "修改")
    @PutMapping
    public ResultRespone updateById(@RequestBody Gdflowconfig gdflowconfig) {
        if (gdflowconfig.getTransferOutType() != null && gdflowconfig.getTabType() != null) {
            return ResultRespone.failed(false, "转入其它流程与接入其它流程不能同时存在");
        }
        Gdflowconfig selGdf = new Gdflowconfig();
        selGdf.setGdlx(gdflowconfig.getGdlx());
        selGdf.setGdxf1(gdflowconfig.getGdxf1());
        selGdf.setGdxf2(gdflowconfig.getGdxf2());
        selGdf.setSfDelete("1");

        Gdflowconfig gf = gdflowconfigService.getOne(Wrappers.query(selGdf).lambda().isNull(gdflowconfig.getGdxf1() == null, Gdflowconfig::getGdxf1).isNull(gdflowconfig.getGdxf2() == null, Gdflowconfig::getGdxf2));
        if (gf != null && !gf.getId().equals(gdflowconfig.getId())) {
            return ResultRespone.failed(false, "名称重复");
        }
        gdflowconfig.setTabTypeName(null);
        gdflowconfig.setTransferOutTypeName(null);
        boolean isupdate = gdflowconfigService.update(new UpdateWrapper<Gdflowconfig>().lambda().set(Gdflowconfig::getSfDelete, 0).eq(Gdflowconfig::getId, gdflowconfig.getId()));
        if (isupdate) {
            gdflowconfig.setId(null);
            return ResultRespone.success(gdflowconfigService.save(gdflowconfig));
        }
        return ResultRespone.success(false, "无记录修改");
    }

    /**
     * 通过id删除
     *
     * @param id id
     * @return ResultRespone
     */
    @ToFlyAppLog(title = "通过id删除")
    @ApiOperation(value = "通过ID删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    @DeleteMapping("/{id}")
    public ResultRespone removeById(@PathVariable Long id) {
        return ResultRespone.success(gdflowconfigService.update(new UpdateWrapper<Gdflowconfig>().lambda().set(Gdflowconfig::getSfDelete, '0').eq(Gdflowconfig::getId, id)));
    }

    /**
     * 通过id批量删除
     *
     * @param ids id
     * @return ResultRespone
     */
    @ToFlyAppLog(title = "通过id批量删除")
    @ApiOperation(value = "通过ID批量删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds")
    public ResultRespone removeById(String ids) {
        return ResultRespone.success(gdflowconfigService.update(new UpdateWrapper<Gdflowconfig>().lambda().set(Gdflowconfig::getSfDelete, '0').in(Gdflowconfig::getId, Arrays.asList(ids.split(",")))));
    }


/**
 * 以下是WJL根据工单优化写的
 */

    /**
     * 新增配置流程多部门多人员
     * @param gdConfigDto 参数
     * @return ResultRespone
     */
    @ApiOperation(value = "新增配置流程(多部门、多人员情况)")
    @PostMapping("/saveConfig")
    public ResultRespone saveConfig(@RequestBody GdConfigDto gdConfigDto) {
        return gdflowconfigService.saveConfig(gdConfigDto);
    }

    @ApiOperation(value = "配置流程查询(多类型、多部门查询)")
//    @GetMapping("/getConfig")
    @GetMapping("/page")
    public ResultRespone getConfig(Page page , String type , String departId , String sfDelete , Long gdlx , String viewType) {
        return ResultRespone.success(gdflowconfigService.getConfig(page , type , departId , sfDelete , gdlx , viewType));
    }

    @ApiOperation(value = "修改配置流程(多部门、多人员情况)")
    @PostMapping("/updateConfig")
    public ResultRespone updateConfig(@RequestBody GdConfigDto gdConfigDto) {
        return gdflowconfigService.updateConfig(gdConfigDto);
    }

    @ApiOperation(value = "获取已配置好的工单类型树形")
    @GetMapping("/getTypeTree")
    public ResultRespone getTypeTree(@RequestParam(value = "viewType", required = false) String viewType, @RequestParam(value = "deptIds", required = false) String deptIds) {
        return gdflowconfigService.getTypeTree(viewType, deptIds);
    }


}
