package com.ics.controller.mobile;


import com.ics.admin.service.IIcsCustomerStaffService;
import com.ics.common.core.domain.IcsCustomerStaff;
import com.ics.common.core.domain.R;
import com.ics.common.utils.bean.BeanUtils;
import com.ics.system.domain.User;
import com.ics.system.domain.form.LoginForm;
import com.ics.system.service.IAccessTokenService;
import com.ics.system.service.ISysLoginService;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;

import java.util.List;

/**
 * 用户登录
 *
 * @author jack
 */
@RestController
@RequestMapping("/auth")
public class LoginAPIController {

    @Autowired
    private IAccessTokenService tokenService;
    @Autowired
    private ISysLoginService sysLoginService;

    @Autowired
    private IIcsCustomerStaffService icsCustomerStaffService;


    @PostMapping("login")
    public R login(@RequestBody LoginForm form) {
        // 用户登录
        IcsCustomerStaff icsCustomerStaff = new IcsCustomerStaff();
        icsCustomerStaff.setMobile(form.getMobile());
        List<IcsCustomerStaff> list = icsCustomerStaffService.selectIcsCustomerStaffList(icsCustomerStaff);
        if(list.size()>0){
            User user = new User();
            BeanUtils.copyBeanProp(user,list.get(0));
            return R.ok(tokenService.createToken(user));
        }else {
            return R.error("登录失败");
        }
    }

}
