package cn.iocoder.yudao.module.inspection.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.core.controller.BaseController;
import cn.iocoder.yudao.module.inspection.query.PartnerListQuery;
import cn.iocoder.yudao.module.inspection.service.AppInspectionPartnerService;
import cn.iocoder.yudao.module.inspection.vo.PartnerListVo;
import cn.iocoder.yudao.module.shop.entity.PartnerDetail;
import cn.iocoder.yudao.module.shop.service.IPartnerDetailService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/admin-api/appInspection/partner")
public class AppPartnerController extends BaseController {
    @Autowired
    private AppInspectionPartnerService partnerList;
    @Autowired
    private IPartnerDetailService detailService;

    /**
     * 查询合作商管理列表
     */
    @GetMapping("/list")
    @TenantIgnore
    public CommonResult partnerList(PartnerListQuery partnerListQuery,
                                    @RequestParam(value = "pageNum" ,required = false ,defaultValue = "1") Integer pageNum,
                                    @RequestParam(value ="pageSize" ,required = false ,defaultValue = "10") Integer pageSize)
    {
        Page<PartnerListVo> page = new Page<>(pageNum, pageSize);
        IPage<PartnerListVo> list = partnerList.partnerList(page,partnerListQuery);
        return success(list);
    }

    /**
     * 获取店铺详情
     */
    @GetMapping("/shopDetail")
    public CommonResult shopDetail(PartnerListQuery partnerListQuery)
    {
     return   success(partnerList.shopDetail(partnerListQuery));
    }

    @PostMapping("/enter")
    public CommonResult enter(@RequestBody PartnerDetail partnerDetail) throws Exception {
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        PartnerDetail partnerDetail1 = detailService.selectPartnerDetailByUserId(loginUser.getId(),"jc");
        if (ObjectUtil.isNotEmpty(partnerDetail1)&&!partnerDetail1.getStatus().equals("3")){
            throw new Exception("不可重复提交");
        }
        //当前登录用户
        if (ObjectUtil.isNotEmpty(partnerDetail1)){
            partnerDetail.setStatus("1");
            partnerDetail.setId(partnerDetail1.getId());
            partnerDetail.setUserId(loginUser.getId());
            detailService.updatePartnerDetail(partnerDetail);
        }else {
            partnerDetail.setStatus("1");
            partnerDetail.setUserId(loginUser.getId());
            detailService.insertPartnerDetail(partnerDetail);
        }

        return success();
    }

    @GetMapping("/enterDetail")
    public CommonResult enterDetail()
    {
        //当前登录用户
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        return success(detailService.selectPartnerDetailByUserId(loginUser.getId(),"jc"));
    }

}
