package cn.pluss.platform.controller.device.order;

import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.base.BaseModelController;
import cn.pluss.platform.device.DeviceTypeService;
import cn.pluss.platform.device.MercDeviceRefundImageService;
import cn.pluss.platform.device.MercOrderDetailService;
import cn.pluss.platform.entity.DeviceType;
import cn.pluss.platform.entity.MercOrderDetail;
import cn.pluss.platform.entity.PhoneConfig;
import cn.pluss.platform.entity.PhoneValidateCode;
import cn.pluss.platform.mapper.DeviceTypeMapper;
import cn.pluss.platform.mapper.PhoneConfigMapper;
import cn.pluss.platform.util.StringUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

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

/**
 * @author SSS
 */
@RequestMapping("/mercOrderDetail")
@Controller
public class MercOrderDetailController extends BaseModelController<MercOrderDetailService, MercOrderDetail> {

    @Resource
    private PhoneConfigMapper phoneConfigMapper;

    @Resource
    private MercDeviceRefundImageService mercDeviceRefundImageService;

    @Resource
    private DeviceTypeMapper deviceTypeMapper;

    @GetMapping("/index")
    public String list(HttpServletRequest request) {
        List<String> codeList = deviceTypeMapper.selectListCodeByShowFlag();
        List<PhoneConfig> phoneList = phoneConfigMapper.selectList(Wrappers.emptyWrapper());
        request.setAttribute("phoneList", phoneList);
        request.setAttribute("codeArr", StringUtil.join(codeList,","));
        return "device/order/detailList";
    }

    /**
     * 分页查询
     * @param page
     * @param condition
     * @return
     */
    @RequestMapping("pageInfo")
    @ResponseBody
    public Result<Page<MercOrderDetail>> pageInfo(Page<MercOrderDetail> page, MercOrderDetail condition) {
        page = baseService.pageInfo(page,condition);
        List<MercOrderDetail> records = page.getRecords();
        if(!records.isEmpty()){
            for (MercOrderDetail detail:records) {
                List<String> picList = mercDeviceRefundImageService.selectListByDetailId(detail.getId());
                detail.setPicList(picList);
            }
        }
        return ResultGenerator.genSuccessResult(page);
    }

    /**
     * 审核退款
     * @param orderDetail
     * @return
     */
    @PostMapping("/auditRefund")
    @ResponseBody
    public Result<Object> refund(MercOrderDetail orderDetail) {
        return baseService.auditRefund(orderDetail);
    }

    @GetMapping("/refundReject/{id}")
    public String refundRejectPage(@PathVariable("id") Long id, HttpServletRequest request) {
        final MercOrderDetail existData = baseService.getById(id);
        request.setAttribute("entity", existData);

        return "device/order/reject";
    }

    @PostMapping("/refundReject")
    @ResponseBody
    public Result<Object> refundReject(MercOrderDetail orderDetail) {
        baseService.refundReject(orderDetail);
        return ResultGenerator.genSuccessResult("已驳回退货/退款申请", null);
    }

    @PostMapping("/refundAgree")
    @ResponseBody
    public Result<Object> refundAgree(Long id) {
        baseService.refundAgree(id);
        return ResultGenerator.genSuccessResult("已同意退款", null);
    }

    /**
     * 获取验证码
     * @param phoneValidateCode
     * @return
     */
    @PostMapping("/getVerifyCode")
    @ResponseBody
    public Result<Object> getVerifyCode(PhoneValidateCode phoneValidateCode) {
        return baseService.getVerifyCode(phoneValidateCode);
    }

    /**
     * 推送物流信息
     * @param ids
     * @return
     */
    @PostMapping("/print")
    @ResponseBody
    public Result<Object> printLogistInfo(@RequestParam("ids[]") List<Long> ids) {
        return baseService.printLogistInfo(ids);
    }

    /**
     * 填写快递单号
     * @param expressNo
     * @return
     */
    @PostMapping("/inputExpressNo")
    @ResponseBody
    public Result<Object> inputExpressNo(Long id,String expressNo) {
        return baseService.inputExpressNo(id,expressNo);
    }
}
