package com.sptg.permissions.biz.controller;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.PageBean;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.permissions.api.dto.RegionAuthorizeDto;
import com.sptg.permissions.api.entity.Organization;
import com.sptg.permissions.api.entity.Region;
import com.sptg.permissions.api.entity.RegionAuthorize;
import com.sptg.permissions.api.feigns.RegionAuthorizeFeign;
import com.sptg.permissions.biz.service.OrgServiceInterface;
import com.sptg.permissions.biz.service.RegionAuthorizeServiceInterface;
import com.sptg.permissions.biz.stream.XqAuthorizeProduceInterface;
import com.sptg.permissions.biz.stream.XqUntyingAuthorizeProduceInterface;
import lombok.RequiredArgsConstructor;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * title: RegionAuthorizeController
 * package: com.sptg.permissions.biz.controller
 * description: 区域授权-控制器
 *
 *                  
 *                 
 */
@RestController
@RequiredArgsConstructor
public class RegionAuthorizeController implements RegionAuthorizeFeign {

    /* -------------------------------------------------- 接口注入 -------------------------------------------------- */

    private final XqAuthorizeProduceInterface xqAuthorizeProduceInterface;
    private final XqUntyingAuthorizeProduceInterface xqUntyingAuthorizeProduceInterface;
    private final OrgServiceInterface orgServiceInterface;
    private final RegionAuthorizeServiceInterface regionAuthorizeServiceInterface;

    /* --------------------------------------------------- 控制器 --------------------------------------------------- */

    @Override
    @GetMapping(value = "/region/authorize/getAuthorizeRegionList")
    public List<Region> getAuthorizeRegionList(@RequestParam(value = "position")String position) {
        return this.regionAuthorizeServiceInterface.selectAuthorizeRegionList(position);
    }

    @Override
    @PostMapping(value = "/region/authorize/getAuthorizeRegion")
    public List<Region> getAuthorizeRegion(@RequestBody RegionAuthorizeDto dto) {
        return this.regionAuthorizeServiceInterface.selectAuthorizeRegion(dto);
    }

    @Override
    @PostMapping(value = "/region/authorize/pagingQuery")
    public Page<RegionAuthorize> pagingQuery(@RequestBody PageBean<RegionAuthorize,RegionAuthorizeDto> pageBean) {
        return this.regionAuthorizeServiceInterface.pagingQuery(pageBean.getDto(),pageBean);
    }

    @Override
    @PostMapping(value = "/region/authorize/batchAdd")
    public ResultBean<Boolean> batchAdd(@RequestBody RegionAuthorizeDto dto) {
        List<Long> orgIdList = dto.getOrgIdList();
        List<RegionAuthorize> authorizeList = new ArrayList<>(orgIdList.size());
        Date now = new Date();
        orgIdList.forEach(orgId -> {
            if (null != orgId){
                //  获取小区信息
                Organization organization = this.orgServiceInterface.selectById(orgId);
                if (null != organization){
                    RegionAuthorize authorize = new RegionAuthorize();
                    //  设置授权机构位置
                    authorize.setOrgPosition(dto.getOrgPosition());
                    //  设置授权小区信息
                    authorize.setXqId(orgId);
                    authorize.setXqName(organization.getName());
                    //  设置小区省市区信息
                    authorize.setRegionProvince(organization.getRegionProvince());
                    authorize.setRegionCity(organization.getRegionCity());
                    authorize.setRegionArea(organization.getRegionArea());
                    authorize.setRegion(organization.getRegion());
                    authorize.setAddress(organization.getAddress());
                    //  创建人,创建时间
                    authorize.setCreateAccountId(dto.getCreateAccountId());
                    authorize.setCreateDate(now);
                    authorizeList.add(authorize);
                }
            }
        });

        Assert.notEmpty(authorizeList,"参数异常");
        //  批量保存
        if (this.regionAuthorizeServiceInterface.insertBatch(authorizeList)) {
            //  通知队列将这些小区的所有设备授权与此机构
            Message<RegionAuthorizeDto> build = MessageBuilder.withPayload(dto).build();
            this.xqAuthorizeProduceInterface.output().send(build);
            return new ResultBean<>(ResultEnum.SUCCESS, true);
        }
        return new ResultBean<>(ResultEnum.ERROR);
    }

    @Override
    @PostMapping(value = "/region/authorize/batchDelete")
    public ResultBean<Boolean> batchDelete(@RequestBody RegionAuthorizeDto dto) {
        Wrapper<RegionAuthorize> wrapper = new EntityWrapper<>();
        wrapper.like("s_org_position",dto.getOrgPosition(), SqlLike.RIGHT);
        wrapper.in("l_xq_id",dto.getOrgIdList());
        boolean result = this.regionAuthorizeServiceInterface.delete(wrapper);
        if (result){
            //  通知队列，删除该机构与这些小区下的设备关联
            Message<RegionAuthorizeDto> build = MessageBuilder.withPayload(dto).build();
            this.xqUntyingAuthorizeProduceInterface.output().send(build);
        }
        return new ResultBean<>(ResultEnum.SUCCESS,result);
    }

    @Override
    @PostMapping(value = "/region/authorize/getAuthorizeOrgList")
    public List<Organization> getAuthorizeOrgList(@RequestBody RegionAuthorizeDto dto) {
        return this.regionAuthorizeServiceInterface.selectAuthorizeOrgList(dto);
    }

    @Override
    @PostMapping(value = "/region/authorize/getAuthorizeOrgListTwo")
    public List<Organization> getAuthorizeOrgListTwo(@RequestBody RegionAuthorizeDto dto) {
        return this.regionAuthorizeServiceInterface.selectAuthorizeOrgListTwo(dto);
    }

    @Override
    @PostMapping(value = "/region/authorize/pagingQueryAuthorizeOrg")
    public Page<Organization> pagingQueryAuthorizeOrg(@RequestBody PageBean<RegionAuthorize,RegionAuthorizeDto> pageBean) {
        return this.regionAuthorizeServiceInterface.pagingQueryAuthorizeOrg(pageBean.getDto(),pageBean);
    }
}
