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.WebInformationFeignService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
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.information.WebInformationAddEntity;
import com.crm.rely.backend.core.entity.web.information.WebInformationDeleteEntity;
import com.crm.rely.backend.core.entity.web.information.WebInformationSearchListEntity;
import com.crm.rely.backend.core.entity.web.information.WebInformationUpdateEntity;
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;

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

    @Autowired
    private WebInformationFeignService webInformationFeignService;

    @Autowired
    private CommonService commonService;

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

        return webInformationFeignService.add(entity);
    }

    /**
     * 删除信息
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/delete")
    public BaseResultDto delete(@RequestBody @Validated WebInformationDeleteEntity entity) throws Exception {


        return webInformationFeignService.delete(entity);
    }

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


        return webInformationFeignService.update(entity);
    }

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


        return webInformationFeignService.pageSearchList(entity);

    }

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

        return webInformationFeignService.single(entity);

    }

    @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/information";
        commonService.validated(file);
        String path = commonService.uploadFile(middlePath, file);

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

    }
}
