package cn.ey.biz.module.system.controller.engagement.business;

import cn.ey.biz.common.pojo.PageParam;
import cn.ey.biz.common.pojo.PageResult;
import cn.ey.biz.common.pojo.R;
import cn.ey.biz.module.system.controller.engagement.business.vo.*;
import cn.ey.biz.module.system.enums.engagement.CompareTypeEnum;
import cn.ey.biz.module.system.enums.engagement.SpecialConditionEnum;
import cn.ey.biz.module.system.enums.engagement.SubConditionEnum;
import cn.ey.biz.module.system.service.engagement.business.EngagementBusinessFieldService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static cn.ey.biz.common.pojo.R.success;

@Api(tags = "项目管理 - 商机字段设置")
@RestController
@RequestMapping("/system/engagement/business/field")
public class EngagementBusinessFieldController {

    @Resource
    private EngagementBusinessFieldService businessFieldService;

    @PostMapping("/create")
    @ApiOperation("创建商机字段")
    @PreAuthorize("@ss.hasPermission('system:engagement-business-field:create')")
    public R<Long> createBusinessField(
            @Validated(value = EngagementBusinessCreateReqVO.Save.class)
            @RequestBody EngagementBusinessCreateReqVO reqVO) {
        Long businessField = businessFieldService.createBusinessField(reqVO);
        return success(businessField);
    }

    @PostMapping("/update")
    @ApiOperation("更新商机字段")
    @PreAuthorize("@ss.hasPermission('system:engagement-business-field:update')")
    public R<Boolean> updateBusinessField(
            @Validated(value = EngagementBusinessCreateReqVO.Update.class)
            @RequestBody EngagementBusinessListReqVO reqVO) {
        businessFieldService.updateBusinessField(reqVO.getList());
        return success(true);
    }

    @GetMapping("/detail")
    @ApiOperation("商机字段详情")
    @PreAuthorize("@ss.hasPermission('system:engagement-business-field:query')")
    public R<EngagementBusinessCreateRespVO> getBusinessFieldDetail(@RequestParam("id") Long id) {
        EngagementBusinessCreateRespVO businessFieldDetail = businessFieldService.getBusinessFieldDetail(id);
        return success(businessFieldDetail);
    }

    @GetMapping("/list")
    @ApiOperation("商机字段列表")
    @PreAuthorize("@ss.hasPermission('system:engagement-business-field:query')")
    public R<List<EngagementBusinessCreateRespVO>> getBusinessFieldList() {
        List<EngagementBusinessCreateRespVO> businessFieldList = businessFieldService.getBusinessFieldList();
        return success(businessFieldList);
    }

    @GetMapping("/operation/record")
    @ApiOperation("最后一次更新人")
    @PreAuthorize("@ss.hasPermission('system:engagement-business-field:query')")
    public R<EngagementBusinessFieldRecordRespVO> getOperationRecord() {
        EngagementBusinessFieldRecordRespVO operationRecord = businessFieldService.getOperationRecord();
        return success(operationRecord);
    }

    @PostMapping("/page/log")
    @ApiOperation("日志分页")
    @PreAuthorize("@ss.hasPermission('system:engagement-business-field:query')")
    public R<PageResult<EngagementBusinessLogRespVO>> getPageLog(@RequestBody PageParam pageParam) {
        PageResult<EngagementBusinessLogRespVO> pageLog = businessFieldService.getPageLog(pageParam);
        return success(pageLog);
    }

    @GetMapping("/special/condition/list")
    @ApiOperation("特殊条件枚举值")
    @PreAuthorize("@ss.hasPermission('system:engagement-business-field:query')")
    public R<List<BusinessFieldSelectOptionValueVO>> specialConditionList() {
        List<BusinessFieldSelectOptionValueVO> contentTypeOption = Arrays.stream(SpecialConditionEnum.values()).map(
                (item) -> new BusinessFieldSelectOptionValueVO(item.getValue(), item.getName())
        ).collect(Collectors.toList());
        return success(contentTypeOption);
    }

    @GetMapping("/sub/condition/list")
    @ApiOperation("子条件枚举值")
    @PreAuthorize("@ss.hasPermission('system:engagement-business-field:query')")
    public R<List<BusinessFieldSelectOptionValueVO>> subConditionList() {
        List<BusinessFieldSelectOptionValueVO> contentTypeOption = Arrays.stream(SubConditionEnum.values()).map(
                (item) -> new BusinessFieldSelectOptionValueVO(item.getValue(), item.getName())
        ).collect(Collectors.toList());
        return success(contentTypeOption);
    }

    @GetMapping("/compare/type/list")
    @ApiOperation("比较运算符枚举值")
    @PreAuthorize("@ss.hasPermission('system:engagement-business-field:query')")
    public R<List<BusinessFieldSelectOptionValueVO>> compareTypeList() {
        List<BusinessFieldSelectOptionValueVO> contentTypeOption = Arrays.stream(CompareTypeEnum.values()).map(
                (item) -> new BusinessFieldSelectOptionValueVO(item.getValue(), item.getName())
        ).collect(Collectors.toList());
        return success(contentTypeOption);
    }

}
