package jnpf.franchisee.controller.web;

import jnpf.base.ActionResult;
import jnpf.franchisee.service.FranchiseeCustomFieldConfigService;
import jnpf.model.franchisee.req.FranchiseeCustomFieldConfigReq;
import jnpf.model.franchisee.req.FranchiseeCustomNameUpdateReq;
import jnpf.model.franchisee.vo.FranchiseeCustomFieldConfigVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * web加盟商自定义字段配置控制器
 */
@RestController
@Validated
@RequestMapping("/web/franchisee/custom-field-config")
public class FranchiseeCustomFieldConfigController {

    @Autowired
    private FranchiseeCustomFieldConfigService franchiseeCustomFieldConfigService;

    /**
     * 新增自定义字段配置
     *
     * @param req 请求参数
     * @return 配置key
     */
    @PostMapping("/add")
    public ActionResult<String> add(@Validated @RequestBody FranchiseeCustomFieldConfigReq req) {
        return ActionResult.success("succeed",franchiseeCustomFieldConfigService.add(req));
    }

    /**
     * 更新自定义字段配置
     *
     * @param req 请求参数
     * @return 配置key
     */
    @PutMapping("/update")
    public ActionResult<String> update(@Validated @RequestBody FranchiseeCustomFieldConfigReq req) {
        return ActionResult.success("succeed",franchiseeCustomFieldConfigService.update(req));
    }

    /**
     * 查询所有自定义字段配置
     *
     * @return 配置列表
     */
    @GetMapping("/query-list")
    public ActionResult<List<FranchiseeCustomFieldConfigVO>> queryList() {
        return ActionResult.success(franchiseeCustomFieldConfigService.queryList());
    }

    /**
     * 查询自定义字段配置详情
     *
     * @return 配置列表
     */
    @GetMapping("/query")
    public ActionResult<FranchiseeCustomFieldConfigVO> query(@RequestParam("key") String key) {
        return ActionResult.success(franchiseeCustomFieldConfigService.query(key));
    }

    /**
     * 查询自定义名称
     *
     * @return 自定义名称
     */
    @GetMapping("/query-custom-name")
    public ActionResult<String> queryCustomName() {
        return ActionResult.success("succeed",franchiseeCustomFieldConfigService.queryCustomName());
    }

    /**
     * 更新自定义名称
     *
     * @param req 更新请求参数
     * @return 更新后的自定义名称
     */
    @PutMapping("/update-custom-name")
    public ActionResult<String> updateCustomName(@Validated @RequestBody FranchiseeCustomNameUpdateReq req) {
        return ActionResult.success("succeed",franchiseeCustomFieldConfigService.updateCustomName(req.getCustomName()));
    }

    /**
     * 删除自定义字段配置
     *
     * @param key 配置key
     * @return 操作结果
     */
    @DeleteMapping("/delete/{key}")
    public ActionResult<Void> delete(@PathVariable("key") @NotBlank(message = "key不能为空") String key) {
        franchiseeCustomFieldConfigService.delete(key);
        return ActionResult.success();
    }

    /**
     * 调整自定义字段排序
     *
     * @param key 按目标顺序传入的字段key列表
     * @return 操作结果
     */
    @PutMapping("/adjust-sort")
    public ActionResult<Void> adjustSort(@RequestBody List<String> key) {
        franchiseeCustomFieldConfigService.adjustSort(key);
        return ActionResult.success();
    }
}
