package cn.com.ctop.finance.payment.controller;

import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.finance.payment.entity.FinancePaymentApplication;
import cn.com.ctop.finance.payment.service.IFinancePaymentApplicationService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 申请垫款表
 */
@Slf4j
@Api(tags = "申请垫款表")
@RestController
@RequestMapping("/financePayment/application")
public class FinancePaymentApplicationController {
    @Autowired
    private IFinancePaymentApplicationService financePaymentApplicationService;
    @Autowired
    private ISysRoleService sysRoleService;

    /**
     * 分页列表查询
     * 销售、销售AM查看自己数据，其他角色查看所有数据
     */
    @ApiOperation(value = "申请垫款表-分页列表查询", notes = "申请垫款表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<FinancePaymentApplication>> queryPageList(FinancePaymentApplication application,
                                                                  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                                  HttpServletRequest req) {
        Result<IPage<FinancePaymentApplication>> result = new Result<>();
        QueryWrapper<FinancePaymentApplication> queryWrapper = QueryGenerator.initQueryWrapper(new FinancePaymentApplication(), req.getParameterMap());
        //查询用户角色
        String roleCode = sysRoleService.getRoleCodeByUserId(application.getUserId());

        if ("saleAssistant".equals(roleCode)) {
            if ("4f6454dbad954a8094013bf4c6d74796".equals(application.getUserId())  || "163a4cc9a72c425e873d36d08e75f2cd".equals(application.getUserId())) {
                List<String> userIds = new ArrayList<>();
                userIds.add("4f6454dbad954a8094013bf4c6d74796");//曹媛 销售助理
                userIds.add("42e137a90ca94cbfa89b21b306cb2e78");//侯加鑫
                userIds.add("959842590bfc4ad38e0aced255177ced");//王维新
                userIds.add("f0190eaabfb4488caa22e123e94dadc8");//高洪哲
                userIds.add("f33798b117a3486a9fae7ba0dab2e0be");//赵欣 销售助理
                userIds.add("515b962ad5544991b17e744df2b87bab");//支春雨 销售助理
                userIds.add("5c6abd3bc1d64a1aa365157f7edd3ecd");//关蕊 销售助理
                userIds.add("2f2d8cd186db4ddcbfc38ead7d116e1c");//许安鑫 销售助理
                userIds.add("02afeb64079043e6b9d6c9581dcef164");//李小治
                userIds.add("ec8f6954b6ae49e7a356d173dd6568d1");//王莹
                userIds.add("96058e7c29f14e4380dfbdddde21175b");//杜鑫艳 销售助理
                userIds.add("fe4965692844468798e4fc265e3cdfc8");//崔泽
                userIds.add("9a99a2e906b64f5082f87c3a60a20b2e");//王明雨
                userIds.add("e2d55e2888e849ed911bc89b72fc8211");//支一淼 销售助理
                userIds.add("102f7edf378b42d2a730d28d11a8cc1e");//张鹤玲  销售助理
                userIds.add("163a4cc9a72c425e873d36d08e75f2cd");//陈心瑞 销售助理
                userIds.add("5398af2d95874451aa012ebb8a15c50d");//陈颖
                userIds.add("b2b6f57dd1a848cd8397e319cd9db1fc");//周菁

                queryWrapper.in("applicanter_id", userIds);
            } else if ("f33798b117a3486a9fae7ba0dab2e0be".equals(application.getUserId())
                    || "515b962ad5544991b17e744df2b87bab".equals(application.getUserId())
                    || "5c6abd3bc1d64a1aa365157f7edd3ecd".equals(application.getUserId())
                    || "96058e7c29f14e4380dfbdddde21175b".equals(application.getUserId())
                    || "2f2d8cd186db4ddcbfc38ead7d116e1c".equals(application.getUserId())
            ) {
                /*销售助理 关蕊组 5c6abd3bc1d64a1aa365157f7edd3ecd：侯加鑫、王维新、高洪哲             */
                List<String> userIds = new ArrayList<>();
                userIds.add("4f6454dbad954a8094013bf4c6d74796");//曹媛 销售助理
                userIds.add("42e137a90ca94cbfa89b21b306cb2e78");//侯加鑫
                userIds.add("959842590bfc4ad38e0aced255177ced");//王维新
                userIds.add("f0190eaabfb4488caa22e123e94dadc8");//高洪哲
                userIds.add("f33798b117a3486a9fae7ba0dab2e0be");//赵欣 销售助理
                userIds.add("515b962ad5544991b17e744df2b87bab");//支春雨 销售助理
                userIds.add("5c6abd3bc1d64a1aa365157f7edd3ecd");//关蕊 销售助理
                userIds.add("2f2d8cd186db4ddcbfc38ead7d116e1c");//许安鑫 销售助理
                userIds.add("02afeb64079043e6b9d6c9581dcef164");//李小治
                userIds.add("ec8f6954b6ae49e7a356d173dd6568d1");//王莹
                userIds.add("96058e7c29f14e4380dfbdddde21175b");//杜鑫艳 销售助理
                queryWrapper.in("applicanter_id", userIds);
            } else if ("e2d55e2888e849ed911bc89b72fc8211".equals(application.getUserId())
                    || "102f7edf378b42d2a730d28d11a8cc1e".equals(application.getUserId())
            ) {
                /*销售助理 陈心瑞组：崔泽、王明雨                 */
                List<String> userIds = new ArrayList<>();
                userIds.add("163a4cc9a72c425e873d36d08e75f2cd");//陈心瑞 销售助理
                userIds.add("fe4965692844468798e4fc265e3cdfc8");//崔泽
                userIds.add("9a99a2e906b64f5082f87c3a60a20b2e");//王明雨
                userIds.add("42e137a90ca94cbfa89b21b306cb2e78");//侯加鑫
                userIds.add("959842590bfc4ad38e0aced255177ced");//王维新
                userIds.add("e2d55e2888e849ed911bc89b72fc8211");//支一淼 销售助理
                userIds.add("102f7edf378b42d2a730d28d11a8cc1e");//张鹤玲  销售助理
                queryWrapper.in("applicanter_id", userIds);
            } else {
                queryWrapper.eq("applicanter_id", application.getUserId());
            }
        }

        if (!Check.isNull(application.getAuditRole())) {
            queryWrapper.like("audit_status", "1");
            if ("finance".equals(application.getAuditRole())) {
                queryWrapper.like("revieweder_id", "financ");
            } else {
                queryWrapper.notLike("revieweder_id", "financ");
            }
        }
        if (!Check.isNull(application.getAdvertiserName())) {
            queryWrapper.like("advertiser_name", application.getAdvertiserName());
        }
        if (!Check.isNull(application.getProductName())) {
            queryWrapper.like("product_name", application.getProductName());
        }
        if (!Check.isNull(application.getAuditStatus())) {
            queryWrapper.eq("audit_status", application.getAuditStatus());
        }
        if (!Check.isNull(application.getPaymentMedia())) {
            queryWrapper.eq("payment_media", application.getPaymentMedia());
        }
        if (!Check.isNull(application.getPaymentType())) {
            queryWrapper.eq("payment_type", application.getPaymentType());
        }
        if (!Check.isNull(application.getRebateType())) {
            queryWrapper.eq("rebate_type", application.getRebateType());
        }
        if (!Check.isNull(application.getStartDate()) && !Check.isNull(application.getEndDate())) {
            queryWrapper.ge("create_time", application.getStartDate() + " 00:00:00");
            queryWrapper.le("create_time", application.getEndDate() + " 23:59:59");
        }
        queryWrapper.orderByDesc("create_time");
        Page<FinancePaymentApplication> page = new Page<FinancePaymentApplication>(pageNo, pageSize);
        IPage<FinancePaymentApplication> pageList = financePaymentApplicationService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    /**
     * 添加申请
     */
    @PostMapping(value = "/add")
    public Result<FinancePaymentApplication> add(@RequestBody JSONObject request) {
        Result<FinancePaymentApplication> result = new Result<>();
        try {
            financePaymentApplicationService.add(request);
            result.success("添加成功！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }

    /**
     * 更新申请
     */
    @PostMapping(value = "/edit")
    public Result<Object> edit(@RequestBody JSONObject request) {
        try {
            FinancePaymentApplication application = JSONObject.parseObject(JSONObject.toJSONString(request), FinancePaymentApplication.class);
            return financePaymentApplicationService.edit(application);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("数据异常");
    }

    /**
     * 更新状态
     */
    @PostMapping(value = "/updateStatus")
    public Result<Object> updateStatus(@RequestBody JSONObject request) {
        try {
            FinancePaymentApplication application = JSONObject.parseObject(JSONObject.toJSONString(request), FinancePaymentApplication.class);
            return financePaymentApplicationService.updateStatus(application);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("数据异常");
    }

    /**
     * 上传临时文件，更新状态
     */
    @PostMapping(value = "/updateTemporaryFiles")
    public Result<Object> updateTemporaryFiles(@RequestBody JSONObject request) {
        try {
            FinancePaymentApplication application = JSONObject.parseObject(JSONObject.toJSONString(request), FinancePaymentApplication.class);
            return financePaymentApplicationService.updateTemporaryFiles(application);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("数据异常");
    }

    /**
     * 通过id删除
     */
    @GetMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id") String id) {
        try {
            financePaymentApplicationService.removeById(id);
        } catch (Exception e) {
            log.error("删除失败", e.getMessage());
            return Result.error("删除失败!");
        }
        return Result.ok("删除成功!");
    }

    /**
     * 通过id查询
     */
    @GetMapping(value = "/queryById")
    public Result<FinancePaymentApplication> queryById(@RequestParam(name = "id", required = true) String id) {
        Result<FinancePaymentApplication> result = new Result<>();
        FinancePaymentApplication financePaymentApplication = financePaymentApplicationService.getById(id);
        if (financePaymentApplication == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(financePaymentApplication);
            result.setSuccess(true);
        }
        return result;
    }

    /**
     * 充值记录
     */
    @GetMapping(value = "/queryPoolInfo")
    public Result<Object> queryPoolInfo(String advertiserId, String productId, String type) {
        return financePaymentApplicationService.queryPoolInfo(advertiserId, productId, type);
    }

    /**
     * 充值记录
     */
    @GetMapping(value = "/queryInfo")
    public Result<Object> queryInfo(String advertiserId, String productId, String type) {
        return financePaymentApplicationService.queryInfo(advertiserId, productId, type);
    }

    /**
     * 获取广告主
     */
    @GetMapping(value = "/getAdvertisers")
    public Result<Object> getAdvertisers(String mediaType) {
        try {
            return financePaymentApplicationService.getAdvertisers(mediaType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("查询异常");
    }

    /**
     * 根据广告主ID集获取产品
     */
    @GetMapping(value = "/getProductByAdvertiserIds")
    public Result<Object> getProductByAdvertiserIds(String ids) {
        try {
            if (Check.isNull(ids)) {
                return Result.error("缺少参数");
            }
            return financePaymentApplicationService.getProductByAdvertiserIds(ids);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("查询异常");
    }


    /**
     * 查询审核流程
     */
    @GetMapping(value = "/queryAuditProcess")
    public Result<Object> queryAuditProcess(Long id) {
        try {
            if (Check.isNull(id)) {
                return Result.error("缺少参数");
            }
            return financePaymentApplicationService.queryAuditProcess(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("查询异常");
    }


    @GetMapping(value = "/queryCwjsPolicyInfo")
    public Result<Object> queryCwjsPolicyInfo(String advertiserName, String productName, String type) {
        return Result.ok();
/*        try {
            if (Check.isNull(advertiserName) || Check.isNull(productName) || Check.isNull(type)) {
                return Result.error("缺少参数");
            }
//            return financePaymentApplicationService.queryCwjsPolicyInfo(advertiserName, productName, type);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("查询异常");*/
    }


}
