package com.crm.web.controller;

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.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.exception.ServiceException;
import com.crm.rely.backend.model.dto.web.custom.analysis.template.WebCustomAnalysisTemplateSearchDto;
import com.crm.rely.backend.model.entity.user.info.UserInfoEntity;
import com.crm.rely.backend.model.entity.web.custom.analysis.template.WebCustomAnalysisTemplateAddEntity;
import com.crm.rely.backend.model.entity.web.custom.analysis.template.WebCustomAnalysisTemplateDeleteEntity;
import com.crm.rely.backend.model.entity.web.custom.analysis.template.WebCustomAnalysisTemplateSearchListEntity;
import com.crm.rely.backend.model.entity.web.custom.analysis.template.WebCustomAnalysisTemplateUpdateEntity;
import com.crm.rely.backend.service.RedisService;
import com.crm.web.service.CommonService;
import com.crm.web.service.WebCustomAnalysisTemplateService;
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("/analysis/template")
public class WebCustomAnalysisTemplateController {

    @Autowired
    private WebCustomAnalysisTemplateService service;

    @Autowired
    private RedisService redisService;

    @Autowired
    private CommonService commonService;

    @PostMapping("/custom/list")
    public BaseResultDto getResultWebCustomAnalysisTemplateSearchList() throws ServiceException {

        WebCustomAnalysisTemplateSearchListEntity entity = new WebCustomAnalysisTemplateSearchListEntity();
        entity.setPage(null);
        List<WebCustomAnalysisTemplateSearchDto> dtos = service.searchList(entity);

        return BaseResultDto.success(dtos);
    }

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

        BaseResultDto<WebCustomAnalysisTemplateSearchDto> result = service.searchSingle(entity.getId());

        return result;
    }

    /**
     * 添加信息
     *
     * @param entity
     * @param infoEntity
     * @return
     * @throws Exception
     */
    @PostMapping("/manager/add")
    public BaseResultDto add(@RequestBody @Validated WebCustomAnalysisTemplateAddEntity entity, UserInfoEntity infoEntity,
                             HttpServletRequest httpServletRequest) throws Exception {

        Runnable runnable = () -> {

            service.add(entity);
        };
        redisService.tryLock(infoEntity, httpServletRequest, runnable, "no_repeat_submit_error");

        return BaseResultDto.success();

    }

    /**
     * 删除信息
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/manager/delete")
    public BaseResultDto delete(@RequestBody @Validated WebCustomAnalysisTemplateDeleteEntity entity, UserInfoEntity infoEntity) throws Exception {

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

    /**
     * 修改信息
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/manager/update")
    public BaseResultDto update(@RequestBody @Validated WebCustomAnalysisTemplateUpdateEntity entity, UserInfoEntity infoEntity,
                                HttpServletRequest httpServletRequest) throws Exception {
        Runnable runnable = () -> {

            service.update(entity);
        };
        redisService.tryLock(infoEntity, httpServletRequest, runnable, "no_repeat_submit_error");

        return BaseResultDto.success();
    }

    /**
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/manager/search/list")
    public ResultWithPagerDto<WebCustomAnalysisTemplateSearchDto> pageSearchList(@RequestBody @Validated WebCustomAnalysisTemplateSearchListEntity entity, UserInfoEntity infoEntity) throws Exception {

        ResultWithPagerDto<WebCustomAnalysisTemplateSearchDto> result = service.pageSearchList(entity);

        return result;
    }

    @PostMapping("/manager/search/single")
    public BaseResultDto<WebCustomAnalysisTemplateSearchDto> single(@RequestBody @Validated SingleLongEntity entity, UserInfoEntity infoEntity) throws Exception {

        BaseResultDto<WebCustomAnalysisTemplateSearchDto> result = service.searchSingle(entity.getId());

        return result;
    }

    @PostMapping("/manager/upload")
    public BaseResultDto upload(@RequestParam(value = "file", required = false) MultipartFile file) throws Exception {
        String middlePath = "/analysis/template";
        commonService.validated(file);
        String path = commonService.uploadFile(middlePath, file);
        return BaseResultDto.success(Constants.UPLOAD_SUCCESS, path);
    }
}
