package com.xueyi.system.receiver.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xueyi.common.core.web.result.AjaxResult;
import com.xueyi.common.core.web.validate.V_A;
import com.xueyi.common.core.web.validate.V_E;
import com.xueyi.common.log.annotation.Log;
import com.xueyi.common.log.enums.BusinessType;
import com.xueyi.common.security.annotation.RequiresPermissions;
import com.xueyi.common.web.entity.controller.BaseController;
import com.xueyi.system.receiver.domain.dto.DmTenantReceiverDto;
import com.xueyi.system.receiver.domain.po.DmTenantReceiverPo;
import com.xueyi.system.receiver.domain.query.DmTenantReceiverQuery;
import com.xueyi.system.receiver.service.IDmTenantReceiverService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 租户接待者管理 业务处理
 *
 * @author xueyi
 */
@RestController
@RequestMapping("/receiver")
public class DmTenantReceiverController extends BaseController<DmTenantReceiverQuery, DmTenantReceiverDto, IDmTenantReceiverService> {

    /** 定义节点名称 */
    @Override
    protected String getNodeName() {
        return "租户接待者" ;
    }

    /**
     * 查询租户接待者列表
     */
    @Override
    @GetMapping("/list")
    @RequiresPermissions(Auth.DM_TENANT_RECEIVER_LIST)
    public AjaxResult list(DmTenantReceiverQuery tenantReceiver) {
        return super.list(tenantReceiver);
    }

    /**
     * 查询租户接待者详细
     */
    @Override
    @GetMapping(value = "/{id}")
    @RequiresPermissions(Auth.DM_TENANT_RECEIVER_SINGLE)
    public AjaxResult getInfo(@PathVariable Serializable id) {
        return super.getInfo(id);
    }

    /**
     * 租户接待者新增
     */
    @Override
    @PostMapping
    @RequiresPermissions(Auth.DM_TENANT_RECEIVER_ADD)
    @Log(title = "租户接待者管理", businessType = BusinessType.INSERT)
    public AjaxResult add(@Validated({V_A.class}) @RequestBody DmTenantReceiverDto tenantReceiver) {
        return super.add(tenantReceiver);
    }

    /**
     * 租户接待者修改
     */
    @Override
    @PutMapping
    @RequiresPermissions(Auth.DM_TENANT_RECEIVER_EDIT)
    @Log(title = "租户接待者管理", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@Validated({V_E.class}) @RequestBody DmTenantReceiverDto tenantReceiver) {
        return super.edit(tenantReceiver);
    }

    /**
     * 租户接待者批量删除
     */
    @Override
    @DeleteMapping("/batch/{idList}")
    @RequiresPermissions(Auth.DM_TENANT_RECEIVER_DEL)
    @Log(title = "租户接待者管理", businessType = BusinessType.DELETE)
    public AjaxResult batchRemove(@PathVariable List<Long> idList) {
        return super.batchRemove(idList);
    }

    /**
     * 获取租户接待者选择框列表
     */
    @Override
    @GetMapping("/option")
    public AjaxResult option() {
        return super.option();
    }


    /**
     * 租户接待者批量新增
     */
    @PostMapping("/batchAdd")
    @RequiresPermissions(Auth.DM_TENANT_RECEIVER_ADD)
    @Log(title = "租户接待者管理", businessType = BusinessType.INSERT)
    public AjaxResult batchAdd(@RequestBody String receivers) {

        List<DmTenantReceiverDto> llist = super.baseService.selectList(new DmTenantReceiverQuery());
        List<Long> ids = llist.stream().map(DmTenantReceiverPo::getId).collect(Collectors.toList());
        if (!ids.isEmpty())
            super.baseService.deleteByIds(ids);

        JSONObject jsonObject = JSONObject.parseObject(receivers);
        List<DmTenantReceiverDto> lists = JSONArray.parseArray(jsonObject.getString("receivers"), DmTenantReceiverDto.class);

        if (!lists.isEmpty()) {
            for (DmTenantReceiverDto po : lists){

                if (null!=po.getId() && po.getId()>0) {
                    po.setDelFlag(0l);
                    baseService.update(po);
                } else {
                    if (StringUtils.isNotEmpty(po.getReceiverPhone()))
                        baseService.insert(po);
                }
            }
        } else {
            return error("参数有误");
        }
        return success();
    }

    interface Auth {
        /** 系统 - 租户接待者管理 - 列表 */
        String DM_TENANT_RECEIVER_LIST = "system:receiver:list";
        /** 系统 - 租户接待者管理 - 详情 */
        String DM_TENANT_RECEIVER_SINGLE = "system:receiver:single";
        /** 系统 - 租户接待者管理 - 新增 */
        String DM_TENANT_RECEIVER_ADD = "system:receiver:add";
        /** 系统 - 租户接待者管理 - 修改 */
        String DM_TENANT_RECEIVER_EDIT = "system:receiver:edit";
        /** 系统 - 租户接待者管理 - 删除 */
        String DM_TENANT_RECEIVER_DEL = "system:receiver:delete";
    }
}
