package com.gree.mall.manager.controller.worker;

import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.annotation.ZfireList;
import com.gree.mall.manager.bean.notice.NoticeWebsitBean;
import com.gree.mall.manager.bean.notice.NoticeWebsitVO;
import com.gree.mall.manager.bean.worker.WebsitWorkerBean;
import com.gree.mall.manager.bean.worker.WorkerTeamVO;
import com.gree.mall.manager.bean.worker.WorkerWarrantyDetail;
import com.gree.mall.manager.bean.worker.WorkerWarrantyVO;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.notice.NoticeWebsitLogic;
import com.gree.mall.manager.logic.worker.WorkerTeamLogic;
import com.gree.mall.manager.logic.worker.WorkerWarrantyLogic;
import com.gree.mall.manager.plus.entity.NoticeWebsit;
import com.gree.mall.manager.plus.entity.WorkerTeam;
import com.gree.mall.manager.plus.entity.WorkerWarrantyRefund;
import com.gree.mall.manager.zfire.bean.WorkerTeamParamBean;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

@Slf4j
@RestController
@Api(value = "质保金", tags = {"质保金账单"})
@RequestMapping(value = "/worker/warranty", produces = "application/json; charset=utf-8")
public class WorkerWarrantyController {

    @Autowired
    private WorkerWarrantyLogic workerWarrantyLogic;


    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "质保金列表")
    public ResponseHelper<IPage<WorkerWarrantyVO>> list(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        IPage<WorkerWarrantyVO> stockBeanIPage = workerWarrantyLogic.list(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        return ResponseHelper.success(stockBeanIPage, new TypeReference<WorkerWarrantyVO>() {});
    }

    @PostMapping("/list/export")
    @ApiOperation(value = "质保金列表导出")
    public void listExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<WorkerWarrantyVO> stockBeanIPage = workerWarrantyLogic.list(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        //3.导出
        FieldUtils.exportData(stockBeanIPage.getRecords(), zfireParam.getExportFields(), request, response);
    }

    @PostMapping("/detail")
    @ApiOperation(value = "详情")
    public ResponseHelper<WorkerWarrantyDetail> detail(@ApiParam(value = "质保id", required = true) @RequestParam String warrantyId

    ) {
        WorkerWarrantyDetail warrantyDetail = workerWarrantyLogic.detail(warrantyId);
        return ResponseHelper.success(warrantyDetail);
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增")
    public ResponseHelper add(@ApiParam(value = "质保金额", required = true) @RequestParam BigDecimal shouldAmount,
                              @ApiParam(value = "每期扣款金额") @RequestParam(required = true) BigDecimal everyAmount,
                              @ApiParam(value = "师傅id") @RequestParam(required = true) List<String> websitUserId
    ) {
        workerWarrantyLogic.add(shouldAmount,everyAmount,websitUserId);
        return ResponseHelper.success();
    }


    @PostMapping("/refund")
    @ApiOperation(value = "退款")
    public ResponseHelper refund(@RequestBody WorkerWarrantyRefund workerWarrantyRefund) {
        workerWarrantyLogic.refund(workerWarrantyRefund);
        return ResponseHelper.success();
    }

    @PostMapping("/import")
    @ApiOperation(value = "新增质保金导入")
    public ResponseHelper<String> importData(
            @RequestPart("file") MultipartFile file
    ) throws Exception {
        workerWarrantyLogic.importData(file);
        return ResponseHelper.success();
    }


}
