package com.crm.web.controller;

import com.crm.rely.backend.core.constant.FeignClientAnnotation;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.model.dto.web.video.WebVideoSearchDto;
import com.crm.rely.backend.model.entity.custom.info.CustomInfoEntity;
import com.crm.rely.backend.model.entity.custom.video.record.CustomVideoRecordAddEntity;
import com.crm.rely.backend.model.entity.custom.video.record.CustomVideoRecordDeleteEntity;
import com.crm.rely.backend.model.entity.web.video.WebVideoAddEntity;
import com.crm.rely.backend.model.entity.web.video.WebVideoDeleteEntity;
import com.crm.rely.backend.model.entity.web.video.WebVideoSearchListEntity;
import com.crm.rely.backend.model.entity.web.video.WebVideoUpdateEntity;
import com.crm.web.service.CustomVideoRecordService;
import com.crm.web.service.WebVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @author: houn
 */
@RestController
@RequestMapping("/web/video")
public class WebVideoController {

    @Autowired
    private WebVideoService webVideoService;

    @Autowired
    private CustomVideoRecordService customVideoRecordService;

    @PostMapping("/custom/validate")
    @FeignClientAnnotation
    public BaseResultDto<Boolean> customValidateRead(CustomInfoEntity infoEntity) throws Exception {

        Boolean vr = webVideoService.validateAllRead(infoEntity.getId());

        return BaseResultDto.success(vr);
    }

    @PostMapping("/custom/read")
    @FeignClientAnnotation
    public BaseResultDto customRead(@RequestBody @Validated CustomVideoRecordAddEntity entity
            , CustomInfoEntity infoEntity) throws Exception {
        entity.setCustomId(infoEntity.getId());
        customVideoRecordService.add(entity);

        return BaseResultDto.success();
    }

    /**
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/custom/list")
    @FeignClientAnnotation
    public BaseResultDto<List<WebVideoSearchDto>> customList(@RequestBody @Validated WebVideoSearchListEntity entity, CustomInfoEntity infoEntity) throws Exception {
        entity.setCustomId(infoEntity.getId());
        entity.setPage(null);
        List<WebVideoSearchDto> result = webVideoService.searchList(entity);

        return BaseResultDto.success(result);
    }

    @PostMapping("/custom/single")
    @FeignClientAnnotation
    public BaseResultDto<WebVideoSearchDto> customSingle(@RequestBody @Validated SingleLongEntity entity) throws Exception {

        BaseResultDto<WebVideoSearchDto> result = webVideoService.searchSingle(entity.getId());

        return result;
    }

    @PostMapping("/custom/record/delete")
    @FeignClientAnnotation
    public BaseResultDto delete(CustomInfoEntity infoEntity) throws Exception {
        CustomVideoRecordDeleteEntity entity = new CustomVideoRecordDeleteEntity();
        entity.setCustomId(infoEntity.getId());
        customVideoRecordService.delete(entity);
        return BaseResultDto.success();
    }

    /**
     * 添加
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/manager/add")
    @FeignClientAnnotation
    public BaseResultDto add(@RequestBody @Validated WebVideoAddEntity entity) throws Exception {

        if (entity.getDeliveryTime() == null) {
            entity.setDeliveryTime(new Date());
        }
        webVideoService.add(entity);
        return BaseResultDto.success();
    }

    /**
     * 删除
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/manager/delete")
    @FeignClientAnnotation
    public BaseResultDto delete(@RequestBody @Validated WebVideoDeleteEntity entity) throws Exception {

        webVideoService.delete(entity);
        return BaseResultDto.success();
    }

    /**
     * 修改
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/manager/update")
    @FeignClientAnnotation
    public BaseResultDto update(@RequestBody @Validated WebVideoUpdateEntity entity) throws Exception {

        webVideoService.update(entity);
        return BaseResultDto.success();
    }

    /**
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/manager/search/list")
    @FeignClientAnnotation
    public ResultWithPagerDto<WebVideoSearchDto> pageSearchList(@RequestBody @Validated WebVideoSearchListEntity entity) throws Exception {

        ResultWithPagerDto<WebVideoSearchDto> result = webVideoService.pageSearchList(entity);

        return result;
    }

    @PostMapping("/manager/search/single")
    @FeignClientAnnotation
    public BaseResultDto<WebVideoSearchDto> single(@RequestBody @Validated SingleLongEntity entity) throws Exception {

        BaseResultDto<WebVideoSearchDto> result = webVideoService.searchSingle(entity.getId());

        return result;
    }
}
