package com.crm.custom.controller;

import com.crm.custom.service.GoldenGoodsService;
import com.crm.rely.backend.core.constant.Constants;
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.golden.goods.GoldenGoodsListDto;
import com.crm.rely.backend.model.entity.custom.info.CustomInfoEntity;
import com.crm.rely.backend.model.entity.golden.goods.GoldenGoodsAddEntity;
import com.crm.rely.backend.model.entity.golden.goods.GoldenGoodsDeleteEntitys;
import com.crm.rely.backend.model.entity.golden.goods.GoldenGoodsSearchEntity;
import com.crm.rely.backend.model.entity.golden.goods.GoldenGoodsUpdateEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/goods")
public class GoldenGoodsController {

    @Autowired
    private GoldenGoodsService goldenGoodsService;

    @PostMapping("/search/list")
    public ResultWithPagerDto<GoldenGoodsListDto> customPageList(@RequestBody GoldenGoodsSearchEntity entity, CustomInfoEntity infoEntity) throws Exception {
        if (infoEntity == null || infoEntity.getId() == null) {
            entity.setCustomId(-1l);
        }else {
            entity.setCustomId(infoEntity.getId());
        }
        return goldenGoodsService.customPageList(entity);
    }

    /**
     * 购买的课程
     */
    @PostMapping("/order/list")
    public ResultWithPagerDto<GoldenGoodsListDto> customOrderList(@RequestBody GoldenGoodsSearchEntity entity, CustomInfoEntity infoEntity) throws Exception {
        if (infoEntity == null || infoEntity.getId() == null) {
            entity.setCustomId(-1l);
        }else {
            entity.setCustomId(infoEntity.getId());
        }
        return goldenGoodsService.customOrderList(entity);
    }

    @PostMapping("/manager/add")
    @FeignClientAnnotation
    public BaseResultDto add(@RequestBody @Validated GoldenGoodsAddEntity entity) throws Exception {
        goldenGoodsService.add(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/manager/delete")
    @FeignClientAnnotation
    public BaseResultDto delete(@RequestBody @Validated GoldenGoodsDeleteEntitys entitys) throws Exception {

        goldenGoodsService.delete(entitys);
        return BaseResultDto.success();
    }

    @PostMapping("/manager/update")
    @FeignClientAnnotation
    public BaseResultDto update(@RequestBody @Validated GoldenGoodsUpdateEntity entity) throws Exception {
        goldenGoodsService.update(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/manager/search/list")
    @FeignClientAnnotation
    public ResultWithPagerDto<GoldenGoodsListDto> pageList(@RequestBody GoldenGoodsSearchEntity entity) throws Exception {
        return goldenGoodsService.searchPageList(entity);
    }

    @PostMapping("/manager/search/single")
    @FeignClientAnnotation
    public BaseResultDto searchSingle(@RequestBody @Validated SingleLongEntity entity) throws Exception {
        return BaseResultDto.success(goldenGoodsService.searchSingle(entity));
    }

    @PostMapping("/manager/upload")
    @FeignClientAnnotation
    public BaseResultDto managerUpload(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest req) throws Exception {

        goldenGoodsService.validated(file);

        String path = goldenGoodsService.uploadFile(file);
        return BaseResultDto.success(Constants.UPLOAD_SUCCESS, path);
    }

}
