package com.fs.crm.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.R;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.crm.domain.CrmCustomerLevel;
import com.fs.crm.param.CrmCustomerLevelParm;
import com.fs.crm.param.CrmCustomerLevelQueryParam;
import com.fs.crm.service.ICrmCustomerLevelService;
import com.fs.crm.vo.CrmCustomerLevelVO;
import com.fs.his.vo.OptionsVO;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/crm/customerLevel")
@AllArgsConstructor
public class CrmCustomerLevelController extends BaseController {

    private final ICrmCustomerLevelService customerLevelService;

    @PreAuthorize("@ss.hasPermi('crm:customerLevel:list')")
    @GetMapping("/list")
    public TableDataInfo list(CrmCustomerLevelQueryParam param) {
        startPage();
        List<CrmCustomerLevelVO> list = customerLevelService.selectCrmCustomerLevelVOList(param);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('crm:customerLevel:add')")
    @Log(title = "客户级别", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Valid @RequestBody CrmCustomerLevelParm param) {
        return toAjax(customerLevelService.saveCustomerLevel(param));
    }

    @PreAuthorize("@ss.hasPermi('crm:customerLevel:edit')")
    @Log(title = "客户级别", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@Valid @RequestBody CrmCustomerLevelParm param) {
        return toAjax(customerLevelService.editCustomerLevel(param));
    }

    @PreAuthorize("@ss.hasPermi('crm:customerLevel:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return AjaxResult.success(customerLevelService.selectCrmCustomerLevelVOById(id));
    }

    @PreAuthorize("@ss.hasPermi('crm:customerLevel:delete')")
    @Log(title = "客户", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/{ids}")
    public AjaxResult delete(@PathVariable Long[] ids) {
        customerLevelService.removeByIds(Arrays.asList(ids));
        return AjaxResult.success();
    }

    @GetMapping("/getCustomerLevelOption")
    public R getCustomerLevelOption(Integer status) {
        LambdaQueryWrapper<CrmCustomerLevel> wrapper = Wrappers.lambdaQuery();
        if (Objects.nonNull(status)) {
            wrapper.eq(CrmCustomerLevel::getStatus, status);
        }
        List<OptionsVO> options = customerLevelService.list(wrapper).stream().map(l -> {
            OptionsVO vo = new OptionsVO();
            vo.setDictLabel(l.getName());
            vo.setDictValue(l.getId());
            return vo;
        }).collect(Collectors.toList());
        return R.ok().put("data", options);
    }
}
