package org.jeecg.modules.shop.customer.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.bean.BeanUtil;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;


import org.jeecg.config.shiro.IgnoreAuth;
import org.jeecg.modules.shop.customer.entity.PartnerCustomer;
import org.jeecg.modules.shop.customer.entity.PartnerCustomerLinkman;
import org.jeecg.modules.shop.customer.entity.dto.PartnerCustomerDTO;
import org.jeecg.modules.shop.customer.service.IPartnerCustomerLinkmanService;
import org.jeecg.modules.shop.customer.service.IPartnerCustomerService;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: partner_customer
 * @Author: jeecg-boot
 * @Date:   2025-07-11
 * @Version: V1.0
 */
@Api(tags="合作伙伴-客户接口")
@RestController
@RequestMapping("/partner/partnerCustomer")
@Slf4j
public class PartnerCustomerController extends JeecgController<PartnerCustomer, IPartnerCustomerService> {
	@Autowired
	private IPartnerCustomerService partnerCustomerService;

	 @Autowired
	 private IPartnerCustomerLinkmanService linkmanService;
	
	/**
	 * 分页列表查询
	 *
	 * @param partnerCustomer
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
//	TODO 客户列表的佣金情况
	@ApiOperation(value="partner_customer-分页列表查询", notes="partner_customer-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<PartnerCustomer>> queryPageList(PartnerCustomer partnerCustomer,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<PartnerCustomer> queryWrapper = QueryGenerator.initQueryWrapper(partnerCustomer, req.getParameterMap());
		Page<PartnerCustomer> page = new Page<PartnerCustomer>(pageNo, pageSize);
		IPage<PartnerCustomer> pageList = partnerCustomerService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param partnerCustomer
	 * @return
	 */
	@ApiOperation(value="partner_customer-添加", notes="partner_customer-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody PartnerCustomerDTO partnerCustomer) {
		partnerCustomerService.addCustomer(partnerCustomer);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param dto
	 * @return
	 */
	@ApiOperation(value="partner_customer-编辑", notes="partner_customer-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody PartnerCustomerDTO dto) {
		partnerCustomerService.updatecustomer(dto);
		return Result.OK("编辑成功!");
	}
	
//	/**
//	 *   通过id删除
//	 *
//	 * @param id
//	 * @return
//	 */
//	@AutoLog(value = "partner_customer-通过id删除")
//	@ApiOperation(value="partner_customer-通过id删除", notes="partner_customer-通过id删除")
//	@RequiresPermissions("partnercustomer:partner_customer:delete")
//	@DeleteMapping(value = "/delete")
//	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
//		partnerCustomerService.removeById(id);
//		return Result.OK("删除成功!");
//	}
	
//	/**
//	 *  批量删除
//	 *
//	 * @param ids
//	 * @return
//	 */
//	@AutoLog(value = "partner_customer-批量删除")
//	@ApiOperation(value="partner_customer-批量删除", notes="partner_customer-批量删除")
//	@RequiresPermissions("partnercustomer:partner_customer:deleteBatch")
//	@DeleteMapping(value = "/deleteBatch")
//	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
//		this.partnerCustomerService.removeByIds(Arrays.asList(ids.split(",")));
//		return Result.OK("批量删除成功!");
//	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
//	TODO 佣金详情
	@ApiOperation(value="partner_customer-通过id查询", notes="partner_customer-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<PartnerCustomerDTO> queryById(@RequestParam(name="id",required=true) String id) {
		PartnerCustomer partnerCustomer = partnerCustomerService.getById(id);
		if(partnerCustomer==null) {
			return Result.error("未找到对应数据");
		}
		PartnerCustomerDTO dto = new PartnerCustomerDTO();
		BeanUtil.copyProperties(partnerCustomer, dto);
		dto.setLinkmanList(linkmanService.list(new QueryWrapper<PartnerCustomerLinkman>().eq("customer_id", id)));
		return Result.OK(dto);
	}
}
