package com.bohuikeji.delivery.backstage.controller.admin;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bohuikeji.delivery.backstage.entity.TCustomerTemplate;
import com.bohuikeji.delivery.backstage.entity.vo.ConfigFormVo;
import com.bohuikeji.delivery.backstage.service.ITCustomerTemplateService;
import com.bohuikeji.frame.module.admin.annotation.InitLog;
import com.bohuikeji.frame.module.admin.base.ControllerBase;
import com.bohuikeji.frame.module.admin.base.InterfaceBase;
import com.bohuikeji.frame.module.admin.entity.TFranchiseeInformation;
import com.bohuikeji.frame.module.admin.entity.back.APIEntity;
import com.bohuikeji.frame.module.admin.entity.sys.SysUser;
import com.bohuikeji.frame.module.admin.entity.web.TableData;
import com.bohuikeji.frame.module.admin.service.ITFranchiseeInformationService;
import com.bohuikeji.frame.module.admin.utils.NumberUtils;
import com.bohuikeji.frame.module.admin.utils.RegUtils;
import com.bohuikeji.frame.module.admin.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 加盟商配置信息表
 * @author 孙劲辉
 */
@Slf4j
@RestController
@RequestMapping("/fran/divided")
@RequiredArgsConstructor
public class TFranDividedController extends ControllerBase {
    final ITFranchiseeInformationService informationService;
    final ITCustomerTemplateService templateService;

    /**
     * 抽成配置
     * @param type
     * @param franId
     * @return
     */
    @GetMapping("/get/type")
    public InterfaceBase getLitGroup(String type, Integer franId) {
        log.info("抽成查询配置：{}",type);
        TFranchiseeInformation franchiseeInformation = informationService.getOne(new QueryWrapper<TFranchiseeInformation>().eq("fran_id",franId));
        log.info("抽成查询配置：{}",franchiseeInformation);
        if("全职".equals(type)){
            return APIEntity.success(franchiseeInformation.getFullTimeDivided());
        }
        if("距离".equals(type)){
            return APIEntity.success(franchiseeInformation.getDistTimeDivided());
        }
        return APIEntity.success(franchiseeInformation.getPartTimeDivided());
    }

    /**
     * 抽成修改
     * @param franId
     * @param type
     * @param sysPrice
     * @param franPrice
     * @param riderPrice
     * @return
     */
    @InitLog("修改客户模板配置")
    @PostMapping("/post/save")
    public APIEntity saveConfig(Integer franId,String type,String sysPrice,String franPrice,String riderPrice,String dist){
        log.info("修改抽成配置：{}",type);
        TFranchiseeInformation franchiseeInformation = informationService.getOne(new QueryWrapper<TFranchiseeInformation>().eq("fran_id",franId));
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("syst",sysPrice);
        jsonObject.put("fran",franPrice);
        jsonObject.put("rider",riderPrice);
        jsonObject.put("courType",type);
        if("距离".equals(type)){
            jsonObject.put("dist",dist);
            franchiseeInformation.setDistTimeDivided(jsonObject);
        }
        if("全职".equals(type)){
            if(StringUtils.isNotBlank(dist)){
                return APIEntity.fail("全职暂不支持设置距离！");
            }
            franchiseeInformation.setFullTimeDivided(jsonObject);
        }
        if("兼职".equals(type)){
            if(StringUtils.isNotBlank(dist)){
                return APIEntity.fail("兼职暂不支持设置距离！");
            }
            franchiseeInformation.setPartTimeDivided(jsonObject);
        }
        return APIEntity.success(informationService.updateById(franchiseeInformation));
    }

    /**
     * 获取登陆用户
     * @return
     */
    @GetMapping("/get/user")
    public APIEntity getUser(){
        SysUser sysUser = getLoginUser();
        return  APIEntity.success(sysUser);
    }

}
