package org.jeecg.modules.system.app.controller.separateAccounts;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.system.app.dto.merchant.AddMerchantRequestDTO;
import org.jeecg.modules.system.app.dto.merchant.FindByMerchantIdResponseDTO;
import org.jeecg.modules.system.app.dto.merchant.FindMerchantRequestDTO;
import org.jeecg.modules.system.app.dto.merchant.FindMerchantResponseDTO;
import org.jeecg.modules.system.service.ISysDepartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description: 商户管理
 * @Author: zx
 * @Date:   2025-07-09
 * @Version: V1.0
 */
@Tag(name="商户管理")
@RestController
@RequestMapping("/merchant")
@Slf4j
public class MerchantController {

    @Autowired
    ISysDepartService iSysDepartService;


    /**
     * 分页列表查询
     * 查询商户列表数据
     * @param findMerchantRequestDTO
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @Operation(summary="商户管理-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<FindMerchantResponseDTO>> queryPageList(FindMerchantRequestDTO findMerchantRequestDTO,
                                                                @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                                @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                                HttpServletRequest req) {
        LoginUser loginUser =(LoginUser)SecurityUtils.getSubject().getPrincipal();
        IPage<FindMerchantResponseDTO> pageList = iSysDepartService.findMerchantPage(pageNo,pageSize,findMerchantRequestDTO.getMerchantName(),loginUser.getOrgCode());
        return Result.OK(pageList);
    }



    /**
     * 新增商户
     * 新增商户
     * @param addMerchantRequestDTO
     * @return
     */
    @Operation(summary="商户管理-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody AddMerchantRequestDTO addMerchantRequestDTO) {
        LoginUser loginUser =(LoginUser)SecurityUtils.getSubject().getPrincipal();
        return iSysDepartService.addMerchnt(addMerchantRequestDTO,loginUser);
    }

    /**
     * 根据id查询商户信息
     * 根据id查询商户信息
     * @param id
     * @return
     */
    @Operation(summary="商户管理-根据id查询商户信息")
    @GetMapping(value = "/findById")
    public Result<FindByMerchantIdResponseDTO> findById(@RequestParam("id") String id) {
        return iSysDepartService.findByMerchntId(id);
    }

    /**
     * 编辑商户信息
     * 编辑商户信息
     * @param addMerchantRequestDTO
     * @return
     */
    @Operation(summary="商户管理-编辑商户信息")
    @PutMapping(value = "/update")
    public Result<String> update(@RequestBody AddMerchantRequestDTO addMerchantRequestDTO) {
        return iSysDepartService.updateMerchntId(addMerchantRequestDTO);
    }

    /**
     * 重置密码
     * 重置密码
     * @param id
     * @return
     */
    @Operation(summary="商户管理-重置密码")
    @GetMapping(value = "/reset")
    public Result<String> reset(@RequestParam("id") String id) {
        return iSysDepartService.resetPassword(id);
    }
}
