package com.zfire.mall.manager.controller.manage.take;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zfire.mall.manager.bean.ExcelData;
import com.zfire.mall.manager.bean.manage.take.TakerRecordBean;
import com.zfire.mall.manager.helper.ResponseHelper;
import com.zfire.mall.manager.logic.manage.take.TakerRecordLogic;
import com.zfire.mall.manager.utils.excel.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@Api(value = "档案", tags ={"提货人档案"} )
@RequestMapping(value = "/taker-record", produces = "application/json; charset=utf-8")
public class TakerRecordController {

    @Resource
    TakerRecordLogic takerRecordLogic;

    @ApiOperation(value = "提货人档案列表")
    @GetMapping("/list")
    public ResponseHelper<IPage<TakerRecordBean>> list(
            @ApiParam(value = "经销商名称") @RequestParam(required = false) String customerName,
            @ApiParam(value = "提货人名称") @RequestParam(required = false) String takerName,
            @ApiParam(value = "提货人电话") @RequestParam(required = false) String phoneKeyword,
            @ApiParam(value = "页号",required = true)@RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true)@RequestParam Integer pageSize
    ) throws Exception {
        IPage<TakerRecordBean> takerRecordBean = takerRecordLogic.listPage(customerName, takerName, phoneKeyword, pageNum, pageSize);
        return ResponseHelper.success(takerRecordBean);
    }

    @ApiOperation(value = "提货人档案列表导出")
    @GetMapping("/export")
    public void export(
            @ApiParam(value = "经销商名称") @RequestParam(required = false) String customerName,
            @ApiParam(value = "提货人名称") @RequestParam(required = false) String takerName,
            @ApiParam(value = "提货人电话") @RequestParam(required = false) String phoneKeyword,
            HttpServletRequest request, HttpServletResponse response
    ) throws Exception {
        IPage<TakerRecordBean> takerRecordBean = takerRecordLogic.listPage(customerName, takerName, phoneKeyword, 1, -1);
        ExcelData excelData = takerRecordLogic.export(takerRecordBean.getRecords());
        ExcelUtils.exportExcel(request,response, "提货人档案.xlsx",excelData);
    }

    @ApiOperation(value = "新增提货人档案")
    @PostMapping("/add")
    public ResponseHelper add(
            @ApiParam(required = true, value = "提货人档案") @RequestBody @Valid TakerRecordBean takerRecordBean
    ) throws Exception {
        takerRecordLogic.add(takerRecordBean);
        return ResponseHelper.success();
    }

    @ApiOperation(value = "停用提货人档案")
    @PostMapping("/stop")
    public ResponseHelper stop(
            @ApiParam(required = true, value = "提货人档案id") @RequestParam String id
    ) {
        takerRecordLogic.stop(id);
        return ResponseHelper.success();
    }

    @ApiOperation(value = "修改提货人档案")
    @PostMapping("/update")
    public ResponseHelper update(
            @ApiParam(required = true, value = "修改提货人档案") @RequestBody @Valid TakerRecordBean takerRecordBean
    ) throws Exception {
        takerRecordLogic.update(takerRecordBean);
        return ResponseHelper.success();
    }

    @ApiOperation(value = "删除提货人档案")
    @PostMapping("/del")
    public ResponseHelper del(
            @ApiParam(required = true, value = "提货人档案id") @RequestParam List<String> ids
    ) throws Exception {
        takerRecordLogic.del(ids);
        return ResponseHelper.success();
    }

    @ApiOperation(value = "发送短信验证码")
    @GetMapping("/send/messageCode")
    public ResponseHelper messageCode(
            @ApiParam(value = "联系电话",required = true) @RequestParam String mobile
    ) throws Exception {
        takerRecordLogic.sendMessageCode(mobile);
        return ResponseHelper.success();
    }
}
