package cn.pluss.platform.controller.audit;


import cn.pluss.platform.SubMerchantApplyOrderService;
import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.base.BaseModelController;
import cn.pluss.platform.entity.SubMerchantApplyOrder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * <p>
 * 子渠道工单信息表 前端控制器
 * </p>
 *
 * @author Djh
 * @since 2020-08-31
 */
@Controller
@RequestMapping("/subMerchantApplyOrder")
public class SubMerchantApplyOrderController extends BaseModelController<SubMerchantApplyOrderService, SubMerchantApplyOrder> {
    @Autowired
    private SubMerchantApplyOrderService subMerchantApplyOrderService;

    @RequestMapping("/toList")
    public String toList() {
        return "subMerchantApplyOrder/list";
    }

    @RequestMapping("/detail")
    public String detail() {
        return "subMerchantApplyOrder/detail";
    }

    @RequestMapping("/pageMoreDetail")
    @ResponseBody
    public Result<Page<Map<String, Object>>> page(Page<Map<String, Object>> page, @RequestParam Map<String, String> condition) {
        Page<Map<String, Object>> mapPage = baseService.pageMoreDetail(page, condition);
        return ResultGenerator.genSuccessResult(mapPage);
    }

    @RequestMapping(value = "/remark", method = RequestMethod.GET)
    public ModelAndView remark(@RequestParam(value = "merchantCode") String merchantCode, Integer channel, ModelAndView view) {
        view.setViewName("subMerchantApplyOrder/remark");
        view.addObject("channel", channel);
        view.addObject("merchantCode", merchantCode);
        return view;
    }

    @RequestMapping(value = "/disable", method = RequestMethod.POST)
    @ResponseBody
    public Result<SubMerchantApplyOrder> disable(String merchantCode, String resultStatus, String channel) {
        QueryWrapper<SubMerchantApplyOrder> queryWrapper = new QueryWrapper<SubMerchantApplyOrder>()
                .eq("merchantCode", merchantCode)
                .eq("channelId", channel);
        SubMerchantApplyOrder subMerchantApplyOrder = new SubMerchantApplyOrder();
        subMerchantApplyOrder.setResultStatus(resultStatus);
        subMerchantApplyOrder.setStatus("2");
        subMerchantApplyOrderService.update(subMerchantApplyOrder, queryWrapper);
        return ResultGenerator.genSuccessResult("修改成功", subMerchantApplyOrder);
    }

    @RequestMapping(value = "/complete", method = RequestMethod.POST)
    @ResponseBody
    public Result<Object> complete(String merchantCode, String channel) {
        LambdaUpdateWrapper<SubMerchantApplyOrder> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SubMerchantApplyOrder::getMerchantCode, merchantCode)
                .eq(SubMerchantApplyOrder::getChannelId, channel)
                .and(wrapper -> {
                    wrapper.ne(SubMerchantApplyOrder::getStatus, "1")
                            .or()
                            .isNull(SubMerchantApplyOrder::getStatus);
                })
                .set(SubMerchantApplyOrder::getResultStatus, SubMerchantApplyOrder.RES_STATUS_COMPLETE)
                .set(SubMerchantApplyOrder::getStatus, SubMerchantApplyOrder.RES_STATUS_COMPLETE);

        boolean updateCount = baseService.update(updateWrapper);
        if (!updateCount) {
            SubMerchantApplyOrder subMerchApplyOrder = new SubMerchantApplyOrder();
            subMerchApplyOrder.setMerchantCode(merchantCode);
            subMerchApplyOrder.setChannelId(Integer.parseInt(channel));
            subMerchApplyOrder.setStatus(SubMerchantApplyOrder.RES_STATUS_SUCCESS);
            subMerchApplyOrder.setResultStatus(SubMerchantApplyOrder.RES_STATUS_SUCCESS);
            subMerchApplyOrder.setSubMchType("WX");
            subMerchApplyOrder.setRemark("已通过手动码或其他方式认证");
            baseService.save(subMerchApplyOrder);
        }

        return ResultGenerator.genSuccessResult("修改成功", null);
    }
}
