package com.crm.manager.controller;

import com.crm.manager.dto.export.UserResignSearchListExportDto;
import com.crm.manager.service.UserResignService;
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.user.info.UserResignSearchListDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.user.info.UserResignAddEntity;
import com.crm.rely.backend.core.entity.user.info.UserResignSearchEntity;
import com.crm.rely.backend.core.entity.user.info.UserResignUpdateEntity;
import com.crm.rely.backend.util.ExportUtil;
import com.crm.rely.backend.util.FileProcessUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/user/resign")
public class UserResignController {

    @Autowired
    private UserResignService userResignService;

    /**
     * 添加离职人员
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    public BaseResultDto add(@RequestBody @Validated UserResignAddEntity entity) throws Exception {
        userResignService.add(entity);
        return BaseResultDto.success();
    }

    /**
     * 修改人员填写信息
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/update")
    public BaseResultDto update(@RequestBody @Validated UserResignUpdateEntity entity) throws Exception {
        userResignService.update(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/search/list")
    public ResultWithPagerDto<UserResignSearchListDto> pageList(@RequestBody UserResignSearchEntity entity) throws Exception {
        return userResignService.searchPageList(entity);
    }

    @PostMapping("/search/single")
    public BaseResultDto searchSingle(@RequestBody @Validated SingleLongEntity entity) throws Exception {
        UserResignSearchListDto result = userResignService.searchSingle(entity);
        return BaseResultDto.success(result);
    }

    @PostMapping("/approve/manage")
    public BaseResultDto approveManage(@RequestBody UserResignUpdateEntity entity, HttpServletRequest httpServletRequest) throws Exception {
        userResignService.approveManage(entity, httpServletRequest);
        return BaseResultDto.success();
    }

    @GetMapping("/search/list/export")
    public void tradeSalesPerformanceExport(
            @RequestParam(name = "resignStatus") Integer resignStatus,
            @RequestParam(name = "userName") String userName,
            @RequestParam(name = "name") String name,
            HttpServletResponse response) throws Exception {
        UserResignSearchEntity entity = new UserResignSearchEntity();
        entity.setName(name);
        entity.setUserName(userName);
        entity.setResignStatus(resignStatus);
        List<UserResignSearchListExportDto> dtos = userResignService.searchPageExport(entity);

        ExportUtil.transferToResponse(FileProcessUtil.genExportFileName("USER_RESIGN_REPORT"), dtos, UserResignSearchListExportDto.class, response);
    }


}
