package com.dgtly.api.controller;

import com.dgtly.apiframework.util.TokenUtil;
import com.dgtly.common.annotation.ApiPassToken;
import com.dgtly.common.constant.UserConstants;
import com.dgtly.common.core.controller.ApiBaseController;
import com.dgtly.common.core.domain.AjaxResult;
import com.dgtly.common.core.domain.ParameterObject;
import com.dgtly.common.core.domain.ResultType;
import com.dgtly.companyext.domain.CompanyReviewed;
import com.dgtly.companyext.service.ICompanyReviewedService;
import com.dgtly.member.domain.MemberFollow;
import com.dgtly.member.service.IMemberFollowService;
import com.dgtly.system.domain.SysCompany;
import com.dgtly.system.service.ISysCompanyService;
import com.dgtly.system.service.ISysUserService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/company")
@ApiPassToken/* 不用验证token  方法上可用*/
public class SysCompanyController extends ApiBaseController {


    @Autowired
    private ISysCompanyService companyService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private IMemberFollowService memberFollowService;

    @Autowired
    private ICompanyReviewedService companyReviewedService;

    @ApiOperation(value = "商家详情")
    @ApiImplicitParam(name = "params" , paramType = "body")
    @PostMapping("/details")
    public Object detail(){
        ParameterObject obj =  getParameterObject();
        obj.checkParameterNotNull("id");
        Long companyId = obj.getLong(" id");
        SysCompany sysCompany =  companyService.selectSysCompanyById(companyId);
        int isFollow = 0;
        /*判断用户是否关注商户*/
        String memberId = TokenUtil.getTokenUserId();
        if(memberId!=null){
            MemberFollow mf= new MemberFollow();
            mf.setCompanyId(companyId);
            mf.setMemberId(Long.parseLong(memberId));
            List<MemberFollow> mfList=  memberFollowService.selectMemberFollowList(mf);
            if(mfList.size()>0){
                isFollow=1;
            }
        }
        if(sysCompany!=null){
            return AjaxResult.success(sysCompany).putKV("isFollow",isFollow);
        }else{
            return AjaxResult.error(ResultType.NUll);
        }
    }


    @ApiOperation(value = "热门商家列表")
    @ApiImplicitParam(name = "params" , paramType = "body")
    @PostMapping("/hotlist")
    public Object getHotList(){
        List<SysCompany> goodsInfos =  companyService.selectCompanyHotList();
        return AjaxResult.success().putKV("list",goodsInfos);
    }


    @ApiOperation(value = "商家入驻")
    @ApiImplicitParam(name = "params" , paramType = "body")
    @PostMapping("/enter")
    public Object enter(){
        ParameterObject obj =  getParameterObject();
        obj.checkParameterNotNull("loginName,password,companyName");
        CompanyReviewed cr = obj.parseBean(CompanyReviewed.class);

        if (UserConstants.USER_NAME_NOT_UNIQUE.equals(userService.checkLoginNameUnique(cr.getLoginName())))
        {
            return AjaxResult.error(300,"新增用户'" + cr.getLoginName() + "'失败，登录账号已存在");
        }
        else if (UserConstants.USER_NAME_NOT_UNIQUE.equals(companyReviewedService.checkLoginNameUnique(cr.getLoginName())))
        {
            return AjaxResult.error(301,"新增用户'" + cr.getLoginName() + "'失败，登录账号在审核列表中已存在");
        }
        else if (cr.getPassword().length() < UserConstants.PASSWORD_MIN_LENGTH
                || cr.getPassword().length() > UserConstants.PASSWORD_MAX_LENGTH)
        {
            return  AjaxResult.error(302,"密码长度为6-19，请重新设置！！");
        }
        int i=  companyReviewedService.insertCompanyReviewed(cr);

        return toAjax(i);
    }

}
