package com.limap.controller.manager;

import java.util.List;
import java.util.Arrays;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.limap.common.core.domain.entity.SysRole;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.limap.common.log.annotation.Log;
import io.swagger.annotations.Api;
import com.limap.common.core.controller.BaseController;
import com.limap.common.core.domain.AjaxResult;
import com.limap.common.enums.BusinessType;
import io.swagger.annotations.ApiOperation;
import com.limap.core.basic.domain.BasicProfitSharingUser;
import com.limap.core.basic.service.IBasicProfitSharingUserService;
import com.limap.common.utils.poi.ExcelUtil;
import com.limap.common.core.page.TableDataInfo;

/**
 * 用户分润基础信息Controller
 *
 * @author jjb
 * @date 2025-04-11
 */
@Api(tags = "用户分润基础信息")
@RestController
@RequestMapping("/basic/sharingUser")
public class BasicProfitSharingUserController extends BaseController {
    @Autowired
    private IBasicProfitSharingUserService basicProfitSharingUserService;

    /**
     * 查询用户分润基础信息列表
     */
    @ApiOperation("查询用户分润基础信息列表")
    @PreAuthorize("@ss.hasPermi('basic:sharingUser:list')")
    @GetMapping("/list")
    public TableDataInfo list(BasicProfitSharingUser basicProfitSharingUser) {
        startPage();
        List<BasicProfitSharingUser> list = basicProfitSharingUserService.selectBasicProfitSharingUserList(basicProfitSharingUser);
        return getDataTable(list);
    }
    /**
     * 导出用户分润基础信息列表
     */
    @ApiOperation("导出用户分润基础信息列表")
    @PreAuthorize("@ss.hasPermi('basic:sharingUser:export')")
    @Log(title = "用户分润基础信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BasicProfitSharingUser basicProfitSharingUser) {
        List<BasicProfitSharingUser> list = basicProfitSharingUserService.selectBasicProfitSharingUserList(basicProfitSharingUser);
        ExcelUtil<BasicProfitSharingUser> util = new ExcelUtil<BasicProfitSharingUser>(BasicProfitSharingUser.class);
        util.exportExcel(response, list, "用户分润基础信息数据");
    }

    /**
     * 获取用户分润基础信息详细信息
     */
    @ApiOperation("获取用户分润基础信息详细信息")
//    @PreAuthorize("@ss.hasPermi('basic:sharingUser:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        BasicProfitSharingUser byId = basicProfitSharingUserService.getById(id);
        if (byId == null) {
            byId = new BasicProfitSharingUser();
        }
        return success(byId);
    }

    /**
     * 新增用户分润基础信息
     */
    @ApiOperation("新增用户分润基础信息")
//    @PreAuthorize("@ss.hasPermi('basic:sharingUser:add')")
    @Log(title = "用户分润基础信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BasicProfitSharingUser basicProfitSharingUser) {
        if(basicProfitSharingUser.getUserId() != null){
            basicProfitSharingUser.setId(basicProfitSharingUser.getUserId());
        }else{
            basicProfitSharingUser.setId(getUserId());
            basicProfitSharingUser.setUserId(getUserId());
        }
        basicProfitSharingUserService.checkRatio(basicProfitSharingUser);
        return toAjax(basicProfitSharingUserService.save(basicProfitSharingUser));
    }

    /**
     * 修改用户分润基础信息
     */
    @ApiOperation("修改用户分润基础信息")
//    @PreAuthorize("@ss.hasPermi('basic:sharingUser:edit')")
    @Log(title = "用户分润基础信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BasicProfitSharingUser basicProfitSharingUser) {
        Long userId = basicProfitSharingUser.getUserId();
        if(userId != null){
            if(!userId.equals(getUserId())){
                List<SysRole> roles = getLoginUser().getUser().getRoles();
                if(roles.stream().noneMatch(role -> role.getRoleId().compareTo(100L) <0)){
                    return error("只有管理员可以修改其他用户的信息");
                }
            }
            basicProfitSharingUser.setId(userId);
        }else{
            basicProfitSharingUser.setId(getUserId());
        }
        if(basicProfitSharingUserService.getById(basicProfitSharingUser.getId()) == null){
            return error("请联系管理员添加用户基础信息");
        }


        basicProfitSharingUserService.checkRatio(basicProfitSharingUser);
        return toAjax(basicProfitSharingUserService.updateById(basicProfitSharingUser));
    }

    /**
     * 删除用户分润基础信息
     */
    @ApiOperation("删除用户分润基础信息")
    @PreAuthorize("@ss.hasPermi('basic:sharingUser:remove')")
    @Log(title = "用户分润基础信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(basicProfitSharingUserService.removeByIds(Arrays.asList(ids)));
    }
}
