package com.fs.app.controller;

import com.alibaba.fastjson.JSON;
import com.fs.app.annotation.Login;
import com.fs.common.core.domain.R;
import com.fs.common.exception.CustomException;
import com.fs.common.utils.StringUtils;
import com.fs.company.domain.CompanyTag;
import com.fs.company.service.ICompanyTagService;
import com.fs.company.service.ICompanyTagUserService;
import com.fs.company.vo.CompanyTagUserVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Api(tags = "企业标签接口")
@RestController
@RequestMapping("/app/companyTag")
@AllArgsConstructor
public class CompanyTagController extends AppBaseController {

    private final ICompanyTagService companyTagService;
    private final ICompanyTagUserService companyTagUserService;

    /**
     * 查询公司标签列表
     */
    @Login
    @GetMapping("/list")
    @ApiOperation("查询公司标签列表")
    public R list(@RequestParam(required = false) String keyword,
                  @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                  @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        log.debug("查询公司标签列表 keyword: {}, pageNum: {}, pageSize: {}", keyword, pageNum, pageSize);

        Map<String, Object> params = new HashMap<>();
        params.put("companyId", getCompanyId());
        if (StringUtils.isNotBlank(keyword)) {
            params.put("keyword", keyword.split(","));
        }

        PageHelper.startPage(pageNum, pageSize);
        List<CompanyTag> list = companyTagService.selectCompanyTagListByMap(params);
        return R.ok().put("data", new PageInfo<>(list));
    }

    /**
     * 新增公司标签
     */
    @Login
    @PostMapping("/add")
    @ApiOperation("新增公司标签")
    public R add(@RequestBody CompanyTag companyTag) throws CustomException {
        log.debug("新增公司标签 companyTag: {}", JSON.toJSONString(companyTag));
        companyTag.setCompanyId(getCompanyId());
        companyTagService.insertCompanyTag(companyTag);
        return R.ok();
    }

    /**
     * 删除公司标签
     */
    @Login
    @PostMapping("/delete")
    @ApiOperation("删除公司标签")
    public R remove(@RequestParam List<Long> tagIds) {
        log.debug("删除公司标签 tagIds: {}", tagIds);
        companyTagService.deleteCompanyTagByTagIds(tagIds);
        return R.ok();
    }

    @Login
    @GetMapping("/tagSubUsers")
    @ApiOperation("标签下会员列表")
    public R tagSubUsers(@RequestParam List<Long> tagId) {
        Map<String, Object> params = new HashMap<>();
        params.put("tagIds", tagId);
        params.put("companyId", getCompanyId());
        return R.ok().put("data", companyTagUserService.selectUserListByMap(params));
    }
}
