package com.chinaweal.aiccs.aiccs.abnormal.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chinaweal.aiccs.aiccs.abnormal.entity.Gzaiccode;
import com.chinaweal.aiccs.aiccs.abnormal.service.IGzaiccodeService;
import com.chinaweal.aiccs.aiccs.dict.entity.CascadeTreeNode;
import com.chinaweal.aiccs.common.util.StringUtils;
import com.chinaweal.aiccs.org.entity.OrgUnits;
import com.chinaweal.aiccs.org.service.AicorgService;
import com.chinaweal.aicorg.model.AICUser;
import com.chinaweal.youfool.framework.springboot.base.query.BaseQuery;
import com.chinaweal.youfool.framework.springboot.rest.RestResult;
import com.chinaweal.youfool.framework.springboot.rest.ResultCode;
import com.chinaweal.youfool.framework.springboot.util.AssertUtils;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 控制器
 * </p>
 *
 * @author chinaweal
 * @since 2021-10-21
 */
@Api(tags = "")
@ApiSort(1)
@RestController
@RequestMapping("/abnormal/gzaiccode")
public class GzaiccodeController {

    @Resource
    private IGzaiccodeService iGzaiccodeService;
    @Autowired
    private AicorgService aicorgService;

    @ApiOperation(value = "1.新增", position = 1)
    @PostMapping("/save")
    public RestResult<Gzaiccode> saveGzaiccode(@RequestBody @Valid Gzaiccode gzaiccode, BindingResult bindingResult) {
        AssertUtils.isBndingValidit(bindingResult);
        gzaiccode = iGzaiccodeService.saveGzaiccode(gzaiccode);
        return RestResult.ok(gzaiccode);
    }

    @ApiOperation(value = "2.删除", position = 2)
    @PostMapping("/delete")
    public RestResult<String> removeGzaiccode(@RequestParam @ApiParam("id") Serializable id) {
        iGzaiccodeService.removeById(id);
        return RestResult.ok();
    }

    @ApiOperation(value = "3.修改", position = 3)
    @PostMapping("/update")
    public RestResult<String> updateGzaiccode(@RequestBody @Valid Gzaiccode gzaiccode, BindingResult bindingResult) {
        AssertUtils.isBndingValidit(bindingResult);
        iGzaiccodeService.updateGzaiccode(gzaiccode);
        return RestResult.ok();
    }

    @ApiOperation(value = "4.查询", position = 4)
    @GetMapping("/get")
    public RestResult<Gzaiccode> getGzaiccode(@RequestParam @ApiParam("id") Serializable id) {
        Gzaiccode gzaiccode = iGzaiccodeService.getById(id);
        return RestResult.ok(gzaiccode);
    }

    @ApiOperation(value = "5.分页查询", position = 5)
    @PostMapping("/list")
    public RestResult<IPage<Gzaiccode>> listGzaiccode(@RequestBody BaseQuery<Gzaiccode> query) {
        IPage<Gzaiccode> data = iGzaiccodeService.listGzaiccode(query);
        return RestResult.ok(data);
    }

    @ApiOperation(value = "6.根据企业主键和权限查询可以办理的人员列表", position = 6)
    @GetMapping("/byPermissionListHandleUserList")
    public RestResult<List<AICUser>> byPermissionListHandleUserList(String pripid, String permission, String unitCode, String unitCn) {
        List<AICUser> userList = iGzaiccodeService.byPermissionListHandleUserList(pripid, permission, unitCode, unitCn);
        return RestResult.ok(userList);
    }

    @ApiOperation(value = "7.获取机关树列表", position = 7)
    @GetMapping("getOrgTreeList")
    public RestResult<?> getOrgTreeList(){
//        List<CascadeTreeNode> list = iGzaiccodeService.getList("3");
//        List<CascadeTreeNode> resultList = new ArrayList<>();
//        if(CollectionUtils.isNotEmpty(list)){
//            resultList = addChild(list);
//        }
        return RestResult.ok(iGzaiccodeService.getOrgTreeList());
    }

    private List<CascadeTreeNode> addChild(List<CascadeTreeNode> parentList){

        for (CascadeTreeNode cascadeTreeNode : parentList) {
            List<CascadeTreeNode> childList = iGzaiccodeService.getChildList(cascadeTreeNode.getId());
            if(CollectionUtils.isNotEmpty(childList)) {
                cascadeTreeNode.setChildren(childList);
            }
            addChild(childList);
        }
        return parentList;
    }

    @ApiOperation(value = "8.获取登记机关--市局区局", position = 7)
    @GetMapping("getRegOrgList")
    public RestResult<?> getRegOrgList(){
        List<CascadeTreeNode> resultList = new ArrayList<>();
        // 市局
        List<CascadeTreeNode> list = iGzaiccodeService.getList("2");
        if(CollectionUtils.isNotEmpty(list)){
            resultList.addAll(list);
        }
        // 区局
        List<CascadeTreeNode> QJlist = iGzaiccodeService.getList("3");
        if(CollectionUtils.isNotEmpty(QJlist)){
            resultList.addAll(QJlist);
        }
        return RestResult.ok(resultList);
    }

    @ApiOperation(value = "9.获取本区监管所", position = 9)
    @PostMapping("getStationByOrg")
    public RestResult<?> getStationByOrg(String regorg){
        if (StringUtils.isBlank(regorg)){
            return RestResult.error(ResultCode.PARAM_IS_BLANK, "获取转办监管所的机关码值为空");
        }
        List<CascadeTreeNode> resultList = new ArrayList<>();

        regorg = regorg.substring(0,6);
        OrgUnits orgUnits = aicorgService.queryByOrgNumber(regorg);

        resultList = iGzaiccodeService.getChildStationList(orgUnits.getOrgUnitId());

        return RestResult.ok(resultList);
    }
}
