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.CompanyHonour;
import com.dgtly.companyext.domain.CompanyReviewed;
import com.dgtly.companyext.service.ICompanyHonourService;
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.ISysDictDataService;
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;
    @Autowired
    private ICompanyHonourService companyHonourService;
    @Autowired
    private ISysDictDataService dictDataService;

    @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);
        /*处理公司类型*/

        String companyTypeName = dictDataService.selectDictLabel("company_type",sysCompany.getCompanyType());

        CompanyHonour ch = new CompanyHonour();
        ch.setCompanyId(companyId);
        List<CompanyHonour> chs = companyHonourService.selectCompanyHonourList(ch);


        int isFollow = 0;
        /*判断用户是否关注商户*/
        try{
            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;
                }
            }
        }catch (Exception e){
        }
        if(sysCompany!=null){
            return AjaxResult.success(sysCompany).putKV("isFollow",isFollow)
                    .putKV("companyTypeName",companyTypeName).putKV("companyHonours",chs);
        }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("/list")
    public Object getList(){
        ParameterObject obj =  getParameterObject();
        startPage(obj);
        SysCompany sc = obj.parseBean(SysCompany.class);
        List<SysCompany> scs =  companyService.selectSysCompanyListExRoot(sc);
        return AjaxResult.success(getDataTable(scs));
    }


    @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(ResultType.LOGINNAMEREPEAT);
        }
        else if (UserConstants.USER_NAME_NOT_UNIQUE.equals(companyReviewedService.checkLoginNameUnique(cr.getLoginName())))
        {
            return AjaxResult.error(ResultType.LOGINNAMEREPEAT);
        }
        else if (cr.getPassword().length() < UserConstants.PASSWORD_MIN_LENGTH
                || cr.getPassword().length() > UserConstants.PASSWORD_MAX_LENGTH)
        {
            return  AjaxResult.error(ResultType.PASSWORD_LENGTH_ERROR);
        }
        int i=  companyReviewedService.insertCompanyReviewed(cr);

        return toAjax(i);
    }

    @ApiOperation(value = "商家入驻公示列表")
    @ApiImplicitParam(name = "params" , paramType = "body",example = "{}")
    @PostMapping("/enterPublicity")
    public Object enterPublicity(){
        ParameterObject obj = getParameterObject();
        startPage(obj);
        CompanyReviewed cr = new CompanyReviewed();
        List<CompanyReviewed> crs =  companyReviewedService.selectEnterPublicityList();
        return AjaxResult.success(getDataTable(crs));
    }


    /**
     * @descption: 统计平台商户入驻数量
     * @param:
     * @return:
     * @auther: LiuLingChao
     * @date: 2020-03-11 11:07
     */
    @PostMapping("/countCompanyInfo")
    public Object countCompanyInfo(){

        int companyNum = companyService.countCompany();
        return AjaxResult.success().putKV("companyNum",companyNum-1);
    }


}
