package com.crm.manager.controller;

import com.crm.login.rely.backend.controller.BaseLoginController;
import com.crm.manager.service.CommonService;
import com.crm.manager.service.activity.WebActivityFeignService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
import com.crm.rely.backend.core.dto.web.activity.WebActivityDropdownDto;
import com.crm.rely.backend.core.dto.web.activity.WebActivitySearchDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.custom.info.InfoEntity;
import com.crm.rely.backend.core.entity.web.activity.*;
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;
import java.util.List;

/**
 * @author: houn
 */
@RestController
@RequestMapping("/web/activity")
public class WebActivityController extends BaseLoginController {

    @Autowired
    private WebActivityFeignService webActivityService;

    @Autowired
    private CommonService commonService;

    /**
     * 添加活动
     *
     * @param entity
     * @param infoEntity
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    public BaseResultDto add(@RequestBody @Validated WebActivityAddEntity entity, InfoEntity infoEntity,
                             HttpServletRequest request) throws Exception {

        return webActivityService.add(entity);
    }

    /**
     * 删除活动
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/delete")
    public BaseResultDto delete(@RequestBody @Validated WebActivityDeleteEntity entity) throws Exception {

        return webActivityService.delete(entity);
    }

    /**
     * 修改活动
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/update")
    public BaseResultDto update(@RequestBody @Validated WebActivityUpdateEntity entity, InfoEntity infoEntity,
                                HttpServletRequest request) throws Exception {

        return webActivityService.update(entity);
    }

    /**
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/search/list")
    public BaseResultDto list(@RequestBody @Validated WebActivitySearchListEntity entity) throws Exception {

        ResultWithPagerDto<WebActivitySearchDto> result = webActivityService.list(entity);

        return result;
    }

    @PostMapping("/search/single")
    public BaseResultDto single(@RequestBody @Validated SingleLongEntity entity) throws Exception {

        BaseResultDto<WebActivitySearchDto> result = webActivityService.single(entity);

        return result;
    }

    @PostMapping("/{method}/upload")
    public BaseResultDto upload(@PathVariable("method") String method, @RequestParam(value = "file",
            required = false) MultipartFile file) throws Exception {

        if (!"add".equals(method) && !"update".equals(method)) {
            return BaseResultDto.error(Constants.SYSTEM_ERROR);
        }
        String middlePath = "/web/activity";
        commonService.validated(file);
        String path = commonService.uploadFile(middlePath, file);

        return BaseResultDto.success(Constants.UPLOAD_SUCCESS, path);
    }

    @PostMapping("/dropdown")
    public BaseResultDto<List<WebActivityDropdownDto>> dropdown(@RequestBody WebActivityDropdownEntity entity,
                                                                InfoEntity infoEntity) throws Exception {
        if (entity.getLang() == null) {
            entity.setLang(infoEntity.getLang());
        }
        return webActivityService.dropdown(entity);
    }
}
