package com.yonge.cooleshow.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;

import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.dto.excel.UserWithdrawalExport;
import com.yonge.cooleshow.biz.dal.enums.AuthStatusEnum;
import com.yonge.cooleshow.biz.dal.enums.BizHttpStatus;
import com.yonge.cooleshow.biz.dal.support.PageUtil;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.mybatis.page.PageInfo;
import com.yonge.toolset.utils.easyexcel.*;
import io.swagger.annotations.*;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import com.yonge.cooleshow.biz.dal.vo.UserWithdrawalVo;
import com.yonge.cooleshow.biz.dal.dto.search.TeacherWithdrawalSearch;
import com.yonge.cooleshow.biz.dal.service.UserWithdrawalService;
import org.springframework.web.multipart.MultipartFile;

import java.lang.reflect.InvocationTargetException;
import java.math.RoundingMode;
import java.util.*;

@RestController
@RequestMapping("/userWithdrawal")
@Api(value = "用户提现", tags = "用户提现")
public class UserWithdrawalController extends BaseController {
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private UserWithdrawalService userWithdrawalService;

    /**
     * 查询单条
     */
    @GetMapping("/detail/{id}")
    @ApiOperation(value = "详情", notes = "传入id")
    @PreAuthorize("@pcs.hasPermissions('userWithdrawal/detail')")
    public HttpResponseResult<UserWithdrawalVo> detail(@PathVariable("id") Long id) {
        return succeed(userWithdrawalService.detail(id));
    }

    /**
     * 查询分页
     */
    @PostMapping("/page")
    @ApiOperation(value = "查询分页", notes = "传入teacherWithdrawalSearch")
    @PreAuthorize("@pcs.hasPermissions('userWithdrawal/page')")
    public HttpResponseResult<PageInfo<UserWithdrawalVo>> page(@RequestBody TeacherWithdrawalSearch query) {
        IPage<UserWithdrawalVo> pages = userWithdrawalService.selectPage(PageUtil.getPage(query), query);
        return succeed(PageUtil.pageInfo(pages));
    }

    /**
     * 查询导出
     */
    @PostMapping("/exportExcel")
    @ApiOperation(value = "查询导出", notes = "传入TeacherWithdrawalSearch")
    @PreAuthorize("@pcs.hasPermissions('userWithdrawal/exportExcel')")
    public void exportExcel(@RequestBody TeacherWithdrawalSearch query) {
        query.setStatus(AuthStatusEnum.DOING);
        List<UserWithdrawalVo> withdrawalVoList = userWithdrawalService.selectList(query);
        List<UserWithdrawalExport> list = new ArrayList<>();
        withdrawalVoList.forEach(o -> {
            UserWithdrawalExport export = new UserWithdrawalExport();
            try {
                BeanUtils.copyProperties(export, o);
                StringBuffer buffer = new StringBuffer(export.getId().toString());
                buffer.append(export.getUserId());
                buffer.append(export.getAmount().setScale(2, RoundingMode.HALF_UP));
                buffer.append(export.getPlantformFee().setScale(2, RoundingMode.HALF_UP));
                buffer.append(export.getActualAmount().setScale(2, RoundingMode.HALF_UP));
                String s = DigestUtils.md5DigestAsHex(buffer.toString().getBytes());
                export.setSign(s);
                list.add(export);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        });
        ExcelUtils.exportExcel(list, "提现列表数据" + System.currentTimeMillis(),
                "列表数据");
    }

    /**
     * 导入
     */
    @PostMapping("/importExcel")
    @ApiOperation(value = "导入", notes = "传入file")
    @PreAuthorize("@pcs.hasPermissions('userWithdrawal/importExcel')")
    public HttpResponseResult<List<ErrMsg>> importExcel(@RequestParam("file") MultipartFile file) {
        if (null == file) {
            return HttpResponseResult.failed("请上传文件");
        }
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        try {
            ExcelDataReader<UserWithdrawalExport> reader = ExcelUtils.getReader(UserWithdrawalExport.class, file);
            userWithdrawalService.importExcel(reader.getDataList(), user.getId());
            return HttpResponseResult.succeed();
        } catch (ExcelException e) {
            return HttpResponseResult.failed(BizHttpStatus.IMPORT.getCode(), e.getErrMsgList(), BizHttpStatus.IMPORT.getMsg());
        }
    }
}