package cn.pluss.platform.controller.agent;

import cn.pluss.platform.agentInfo.AgentInfoService;
import cn.pluss.platform.api.PageInfo;
import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.entity.MerchantImage;
import cn.pluss.platform.entity.UserApp;
import cn.pluss.platform.merchantBankCard.MerchantBankCardService;
import cn.pluss.platform.merchantImage.MerchantImageService;
import cn.pluss.platform.userApp.UserAppService;
import cn.pluss.platform.util.PageUtils;
import cn.pluss.platform.util.StringUtil;
import cn.pluss.platform.vo.AgentInfo;
import cn.pluss.platform.vo.AgentStaffVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

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

@Controller
@RequestMapping("agentInfo")
public class AgentInfoController {

    @Autowired
    private AgentInfoService agentInfoService;

    @Autowired
    private UserAppService userAppService;

    @Autowired
    private MerchantImageService merchantImageService;

    @Autowired
    private MerchantBankCardService merchantBankCardService;

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = "/list")
    public String index() {
        return "agent/list";
    }

    @GetMapping("/page")
    @ResponseBody
    public Result<PageInfo<AgentInfo>> page(Page<AgentInfo> page, AgentInfo agentInfo) {
        if ("".equals(agentInfo.getIsFixedRate())) {
            agentInfo.setIsFixedRate(null);
        }
        Page<AgentInfo> pageData = agentInfoService.pageData(page, agentInfo);
        return ResultGenerator.genSuccessResult(PageUtils.page(pageData));
    }

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = {"/detail/{userId}", "/detail"})
    public String detail(HttpServletRequest request, @PathVariable(value = "userId", required = false) Long userId) {
        AgentInfo agentInfo;
        if (userId != null) {
            agentInfo = new AgentInfo().setUserId(userId);
            agentInfo = agentInfoService.getOne(agentInfo);
        } else {
            agentInfo = new AgentInfo();
            agentInfo.setRate(BigDecimal.valueOf(25));
        }
        request.setAttribute("entity", agentInfo);
        return "agent/detail";
    }

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = {"/part/{userId}"})
    public String part(HttpServletRequest request, @PathVariable(value = "userId", required = false) Long userId) {
        //超级管理商结算信息：
        if (userId != null) {
            //超级管理商基本信息
            QueryWrapper<UserApp> userAppQueryWrapper = new QueryWrapper<UserApp>().eq("userId", userId);
            UserApp userAppServiceOne = userAppService.getOne(userAppQueryWrapper);
            request.setAttribute("userApp", userAppServiceOne);
            UserApp appInfo = userAppService.queryUserAppJoinByUserId(userId);
            if (appInfo != null) {
                if (StringUtils.isNotEmpty(appInfo.getMerchantCode())) {
                    MerchantImage image = new MerchantImage();
                    image.setMerchantCode(appInfo.getMerchantCode());
                    if (appInfo.getMerchantBaseInfo() != null) {
                        request.setAttribute("merchantInfo", appInfo.getMerchantBaseInfo());
                    }
                    if (appInfo.getMerchantBankCard() != null) {
                        request.setAttribute("mercBankCard", appInfo.getMerchantBankCard());
                    }
                    List<MerchantImage> imageList = merchantImageService.queryMerchantImageList(image);
                    String merchantImage1 = null;
                    String merchantImage2 = null;
                    String merchantImage3 = null;
                    String merchantImage6 = null;
                    String merchantImage8 = null;
                    String merchantImage9 = null;
                    String merchantImage98 = null;
                    if (imageList != null && !imageList.isEmpty()) {
                        for (MerchantImage m : imageList) {
                            switch (m.getPhotoType()) {
                                case "01":
                                    merchantImage1 = m.getPicUrl();
                                    break;
                                case "02":
                                    merchantImage2 = m.getPicUrl();
                                    break;
                                case "03":
                                    merchantImage3 = m.getPicUrl();
                                    break;
                                case "06":
                                    merchantImage6 = m.getPicUrl();
                                    break;
                                case "08":
                                    merchantImage8 = m.getPicUrl();
                                    break;
                                case "09":
                                    merchantImage9 = m.getPicUrl();
                                    break;
                                case "98":
                                    merchantImage98 = m.getPicUrl();
                                    break;
                                default:
                                    break;
                            }
                        }
                        request.setAttribute("merchantImage1", merchantImage1);
                        request.setAttribute("merchantImage2", merchantImage2);
                        request.setAttribute("merchantImage3", merchantImage3);
                        request.setAttribute("merchantImage6", merchantImage6);
                        request.setAttribute("merchantImage8", merchantImage8);
                        request.setAttribute("merchantImage9", merchantImage9);
                        request.setAttribute("merchantImage98", merchantImage98);
                    }
                }
            }
        }
        return "agent/particulars";
    }

    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public Result<AgentInfo> saveOrUpdate(@RequestBody AgentInfo agentInfo) {
        agentInfoService.saveOrUpdate(agentInfo);
        return ResultGenerator.genSuccessResult("保存成功", agentInfo);
    }

    @PostMapping("/delete")
    public Result<AgentInfo> del(@RequestBody AgentInfo agentInfo) {
        agentInfoService.delete(agentInfo);
        return ResultGenerator.genSuccessResult("删除成功", agentInfo);
    }

    @PostMapping("/saveAgentInfo")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public Result<String> saveAgentInfo(UserApp userApp) {
        merchantBankCardService.updateMerchantBankCardUserId(userApp.getMerchantBankCard());
        if (StringUtil.isNotEmpty(userApp.getUserId())) {
            userAppService.updateUserAppUserId(userApp);
        }
        return ResultGenerator.genSuccessResult("保存成功");
    }

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = "/subAgentList")
    public String subAgentList() {
        return "agent/subAgentList";
    }

    @GetMapping("/subAgentPage")
    @ResponseBody
    public Result<PageInfo<AgentInfo>> subAgentPage(Page<AgentInfo> page, AgentInfo agentInfo) {
        Page<AgentInfo> pageData = agentInfoService.pageSubData(page, agentInfo);
        return ResultGenerator.genSuccessResult(PageUtils.page(pageData));
    }

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = "/staffList")
    public String staffList() {
        return "agent/staffList";
    }

    @GetMapping("/staffPage")
    @ResponseBody
    public Result<PageInfo<AgentStaffVO>> staffPage(Page<AgentStaffVO> page, AgentStaffVO agentStaffVO, OrderItem orderItem) {
        Page<AgentStaffVO> userAppPage = agentInfoService.staffPage(page, agentStaffVO, orderItem);
        return ResultGenerator.genSuccessResult(PageUtils.page(userAppPage));
    }
}
