package com.goafanti.admin.controller;

import com.goafanti.admin.bo.SalesTeamBo;
import com.goafanti.admin.service.SalesTeamService;
import com.goafanti.common.bo.Result;
import com.goafanti.common.controller.CertifyApiController;
import com.goafanti.common.model.SalesTeam;
import com.goafanti.common.utils.ParamUtils;
import com.goafanti.core.mybatis.page.Pagination;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

/**
 * 营销团队(SalesTeam)表控制层
 *
 * @author makejava
 * @since 2024-12-09 11:18:43
 */
@Controller
@RequestMapping(value = "/api/admin/salesTeam")
public class AdminSalesTeamController extends CertifyApiController {
    /**
     * 服务对象
     */
    @Resource
    private SalesTeamService salesTeamService;


    /**
     * 新增数据
     *
     * @param in 实体
     * @return 新增结果
     */
    @PostMapping(value = "/add")
    public Result add(@Validated SalesTeam in, BindingResult bindingResult) {
        Result res = new Result();
        if (bindingResult.hasErrors()) {
            String param=bindingResult.getFieldError().getField();
            res.getError().add(buildError(ParamUtils.getParamMsg(in,param),ParamUtils.getParamName(in,param)));
            return res;
        }
        if (salesTeamService.checkName(in)){
            res.getError().add(buildError("团队名称已经存在。"));
            return res;
        }
        return res.data(this.salesTeamService.insert(in));
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/get")
    public Result<SalesTeam> queryById(Integer id) {
        return new Result<>().data(this.salesTeamService.queryById(id));
    }


    /**
     * 编辑数据
     *
     * @param salesTeam 实体
     * @return 编辑结果
     */
    @PostMapping("/update")
    public Result edit(SalesTeam salesTeam) {
        return new Result<>().data(this.salesTeamService.update(salesTeam));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @GetMapping("/delete")
    public Result deleteById(Integer id) {
        return new Result<>().data(this.salesTeamService.deleteById(id));
    }

    /**
     * 分页列表查询
     *
     * @param in 参数
     */
    @GetMapping("/pageList")
    public Result<Pagination<SalesTeamBo>> pageList(SalesTeam in, Integer pageNo, Integer pageSize) {
        return new Result<>().data(this.salesTeamService.pageList(in, pageNo, pageSize));
    }

    /**
     * 列表查询
     *
     * @param lvl 层级
     */
    @GetMapping("/list")
    public Result<Pagination<SalesTeamBo>> list(Integer lvl) {
        return new Result<>().data(this.salesTeamService.list(lvl));
    }

    /**
     * 团队层级列表查询
     *
     */
    @GetMapping("/lvlList")
    public Result<List<SalesTeamBo>> lvlList() {
        return new Result<>().data(this.salesTeamService.lvlList());
    }

}

