package com.kingtom.shengtai.api.controller;

import java.util.List;
import java.util.Map;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.kingtom.kirin.api.security.SaSecurityUtils;
import com.kingtom.kirin.app.security.base.SessionData;
import com.kingtom.kirin.core.common.base.PageInfo;
import com.kingtom.kirin.core.common.base.Result;
import com.kingtom.kirin.core.common.utils.CollectionUtils;
import com.kingtom.kirin.core.common.utils.JsonUtils;
import com.kingtom.shengtai.api.mapper.SrmMapper;
import com.kingtom.shengtai.api.model.SrmCooperateDTO;
import com.kingtom.shengtai.app.srm.model.SrmCooperate;
import com.kingtom.shengtai.app.srm.model.SrmCooperateDO;
import com.kingtom.shengtai.app.srm.model.SrmSupplier;
import com.kingtom.shengtai.app.srm.service.ISrmCooperateService;
import com.kingtom.shengtai.app.srm.service.ISrmSupplierService;
import com.mybatisflex.core.paginate.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 供应商合作记录表 控制层。
 *
 * @author wany
 * @since 2023-12-21
 */
@Tag(name = "SrmCooperateController", description = "供应商合作记录表 控制层。")
@RestController
@RequestMapping("/srmCooperates")
public class SrmCooperateController{

    @Autowired
    private ISrmCooperateService iSrmCooperateService;

    @Autowired
    private ISrmSupplierService iSrmSupplierService;

    /**
     * 添加 供应商合作记录表。
     *
     * @param dto 供应商合作记录表
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @Operation(summary = "添加 供应商合作记录表。", description = "添加 供应商合作记录表。")
    @PostMapping("/")
    @SaCheckPermission(value = {"AuthMenu:srm_supplier:Execute"})
    public Result<SrmCooperateDTO> save(@RequestBody SrmCooperateDTO dto){
        SrmCooperate convert = SrmMapper.INSTANCE.convert(dto);
        SrmCooperateDO srmCooperateDO = iSrmCooperateService.create(convert);
        return Result.ok(SrmCooperateDTO.convert(srmCooperateDO));
    }

    /**
     * 根据主键删除 供应商合作记录表。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @Parameter(name = "id", description = "主键", in = ParameterIn.PATH, required = true)
    @Operation(summary = "根据主键删除 供应商合作记录表。", description = "根据主键删除 供应商合作记录表。")
    @PostMapping("/{id}/")
    @SaCheckPermission(value = {"AuthMenu:srm_supplier:Execute"})
    public Result<String> remove(@PathVariable String id){
        iSrmCooperateService.deleteById(id);
        return Result.ok(null);
    }

    /**
     * 根据主键批量删除供应商信息表。
     *
     * @param ids 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @Operation(summary = "根据主键批量删除供应商信息表。", description = "根据主键批量删除供应商信息表。")
    @PostMapping("/remove_batch")
    @SaCheckPermission(value = {"AuthMenu:srm_supplier:Execute"})
    public Result<String> remove(@RequestBody List<String> ids){
        iSrmCooperateService.deleteByIds(ids);
        return Result.ok(null);
    }

    /**
     * 根据id更新供应商合作记录
     *
     * @param dto 供应商合作记录表
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @Operation(description = "根据id更新供应商合作记录")
    @PostMapping("/{id}/info")
    @SaCheckPermission(value = {"AuthMenu:srm_supplier:Execute"})
    public Result<SrmCooperateDTO> update(@RequestBody SrmCooperateDTO dto){
        SrmCooperate convert = SrmMapper.INSTANCE.convert(dto);
        SrmCooperateDO srmCooperateDO = iSrmCooperateService.update(convert);
        return Result.ok(SrmCooperateDTO.convert(srmCooperateDO));
    }

    /**
     * 根据id获取供应商合作记录
     *
     * @param id 供应商合作记录表主键
     * @return 供应商合作记录表详情
     */
    @Operation(description = "根据id获取供应商合作记录")
    @GetMapping("/{id}/info")
    public Result<SrmCooperateDTO> getInfo(@PathVariable String id){
        SrmCooperateDO srmCooperateDO = iSrmCooperateService.findById(id);
        return Result.ok(SrmCooperateDTO.convert(srmCooperateDO));
    }

    /**
     * 分页查询供应商合作记录
     *
     * @param query    查询条件
     * @param pageNum  当前页码
     * @param pageSize 每页数量
     * @return 分页对象
     */
    @Operation(description = "分页查询供应商合作记录")
    @GetMapping("/page")
    @SaCheckPermission(value = {"AuthMenu:srm_supplier:Execute"})
    public Result<PageInfo<SrmCooperateDTO>> page(String query, Integer pageNum, Integer pageSize){
        Map<String, Object> object = JsonUtils.toObject(query, Map.class);
        String search = object.get("search") == null ? null : object.get("search").toString();
        String supplierId = object.get("supplierId") instanceof String ? object.get("supplierId").toString() : null;
        Page<SrmCooperateDO> page = iSrmCooperateService.page(search, supplierId, pageNum, pageSize);
        return Result.ok(CollectionUtils.convertPage(page, SrmCooperateDTO::convert));
    }

    @Operation(description = "分页查询查询我的供应商合作记录")
    @GetMapping("/self/page")
    @SaCheckRole(value = {"supplier"})
    public Result<PageInfo<SrmCooperateDTO>> getSelfPage(String query, Integer pageNum, Integer pageSize){
        SessionData sessionData = SaSecurityUtils.findSessionData();
        String userId = sessionData.getUserId();
        SrmSupplier supplier = iSrmSupplierService.findByPerson(userId);
        Page<SrmCooperateDO> page = new Page<>();
        if(supplier == null){
            return Result.ok(CollectionUtils.convertPage(page, SrmCooperateDTO::convert));
        }
        Map<String, Object> object = JsonUtils.toObject(query, Map.class);
        String search = object.get("search") == null ? null : object.get("search").toString();
        page = iSrmCooperateService.page(search, supplier.getId(), pageNum, pageSize);
        return Result.ok(CollectionUtils.convertPage(page, SrmCooperateDTO::convert));
    }

}
