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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.system.app.dto.separateAccounts.FindAccountRequestDTO;
import org.jeecg.modules.system.app.dto.separateAccounts.FindAccountResponseDTO;
import org.jeecg.modules.system.app.entity.SeparateAccounts;
import org.jeecg.modules.system.app.service.ISeparateAccountsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.Arrays;

/**
 * @Description: 分账管理
 * @Author: jeecg-boot
 * @Date:   2025-07-10
 * @Version: V1.0
 */
@Tag(name="分账管理")
@RestController
@RequestMapping("/separateAccounts/separateAccounts")
@Slf4j
public class SeparateAccountsController extends JeecgController<SeparateAccounts, ISeparateAccountsService> {
	@Autowired
	private ISeparateAccountsService separateAccountsService;
	
	/**
	 * 分页列表查询
	 *
	 * @param findAccountRequestDTO
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "分账管理-分页列表查询")
	@Operation(summary="分账管理-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<FindAccountResponseDTO>> queryPageList(FindAccountRequestDTO findAccountRequestDTO,
														 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
														 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
														 HttpServletRequest req) {


		Page<FindAccountResponseDTO> page = new Page<FindAccountResponseDTO>(pageNo, pageSize);
		IPage<FindAccountResponseDTO> pageList = separateAccountsService.findPage(page, findAccountRequestDTO);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param separateAccounts
	 * @return
	 */
	@AutoLog(value = "分账管理-添加")
	@Operation(summary="分账管理-添加")
	@RequiresPermissions("separateAccounts:nm_separate_accounts:add")
	@PostMapping(value = "/add")
	public Result<String> add(@Valid @RequestBody SeparateAccounts separateAccounts) {
		SeparateAccounts separateAccountsOld =separateAccountsService.findByDeptId(separateAccounts.getDeptId(),null);
		if (separateAccountsOld!=null){
			return Result.error("添加失败,该商铺分账已存在");
		}
		BigDecimal sum = separateAccounts.getPtSeparateAccounts().add(separateAccounts.getShSeparateAccounts());
		if (!(sum.compareTo(new BigDecimal("30")) <= 0)){
			return Result.error("添加失败,平台与商户的分账比例不可以大于30");
		}
		separateAccountsService.save(separateAccounts);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param separateAccounts
	 * @return
	 */
	@AutoLog(value = "分账管理-编辑")
	@Operation(summary="分账管理-编辑")
	@RequiresPermissions("separateAccounts:nm_separate_accounts:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody SeparateAccounts separateAccounts) {
		if (StringUtils.isEmpty(separateAccounts.getId())){
			return Result.error("添加失败,请选择商户");
		}
		SeparateAccounts separateAccountsOld =separateAccountsService.findByDeptId(separateAccounts.getDeptId(),separateAccounts.getId());
		if (separateAccountsOld!=null){
			return Result.error("添加失败,该商铺分账已存在");
		}
		BigDecimal sum = separateAccounts.getPtSeparateAccounts().add(separateAccounts.getShSeparateAccounts());
		if (!(sum.compareTo(new BigDecimal("30")) <= 0)){
			return Result.error("添加失败,平台与商户的分账比例不可以大于30");
		}
		separateAccountsService.updateById(separateAccounts);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "分账管理-通过id删除")
	@Operation(summary="分账管理-通过id删除")
	@RequiresPermissions("separateAccounts:nm_separate_accounts:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		separateAccountsService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "分账管理-批量删除")
	@Operation(summary="分账管理-批量删除")
	@RequiresPermissions("separateAccounts:nm_separate_accounts:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.separateAccountsService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "分账管理-通过id查询")
	@Operation(summary="分账管理-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<SeparateAccounts> queryById(@RequestParam(name="id",required=true) String id) {
		SeparateAccounts separateAccounts = separateAccountsService.getById(id);
		if(separateAccounts==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(separateAccounts);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param separateAccounts
    */
    @RequiresPermissions("separateAccounts:nm_separate_accounts:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SeparateAccounts separateAccounts) {
        return super.exportXls(request, separateAccounts, SeparateAccounts.class, "分账管理");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("separateAccounts:nm_separate_accounts:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SeparateAccounts.class);
    }

}
