package cn.pluss.platform.controller.staff;

import cn.pluss.platform.api.Result;
import cn.pluss.platform.controller.BaseNoModelController;
import cn.pluss.platform.dto.StaffDTO;
import cn.pluss.platform.entity.MerchantStore;
import cn.pluss.platform.entity.UserApp;
import cn.pluss.platform.merchant.MerchantBaseInfoService;
import cn.pluss.platform.merchantStore.MerchantStoreService;
import cn.pluss.platform.userApp.UserAppService;
import cn.pluss.platform.vo.UserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 员工管理
 *
 * @author Administrator
 */
@Controller
@RequestMapping("staff")
public class StaffController extends BaseNoModelController {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private UserAppService userAppService;

    @Autowired
    private MerchantStoreService merchantStoreService;

    @Autowired
    private MerchantBaseInfoService merchantBaseInfoService;

    /**
     * 引导页
     *
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        String userType = request.getParameter("userType");
        request.setAttribute("userType", userType);
        String merchantCode = (String) request.getSession().getAttribute("merchantCodeSession");
        MerchantStore merchantStore = new MerchantStore();
        merchantStore.setMerchantCode(merchantCode);
        List<MerchantStore> merchantStoreList = merchantStoreService.queryMerchantStoreList(merchantStore);
        if (merchantStoreList != null && merchantStoreList.size() > 0) {
            request.setAttribute("merchantStoreList", merchantStoreList);
        }
        return "staff/userAppList";
    }

    /**
     * 分页查询
     */
    @RequestMapping(value = "/queryUserAppByPage", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> queryUserAppByPage(@RequestParam(defaultValue = "1") Integer currPage, Integer size,
                                                  UserInfoVO userInfoVO) {
        return userAppService.getStaffPage(currPage,size,userInfoVO);
    }

    /**
     * 查询详情
     */
    @GetMapping(value = {"/queryUserAppBaseInfo/{id}", "/queryUserAppBaseInfo"})
    public String queryUserAppBaseInfo(HttpServletRequest request, @PathVariable(value = "id", required = false) Long id, Integer pageIndex) {
        if (id != null && id != 0) {
            UserApp userApp = new UserApp();
            userApp.setId(id);
            userApp = userAppService.queryUserApp(userApp);
            request.setAttribute("userApp", userApp);

            MerchantStore merchantStore = new MerchantStore();
            merchantStore.setMerchantCode(userApp.getMerchantCode());
            List<MerchantStore> merchantStoreList = merchantStoreService.queryMerchantStoreList(merchantStore);
            request.setAttribute("merchantStoreList", merchantStoreList);

            request.setAttribute("pageIndex", pageIndex);
        }
        return "staff/userAppBaseInfo";
    }

    /**
     * 删除对象
     *
     * @throws IOException
     */
    @RequestMapping(value = "/deleteUserApp", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deleteUserApp(Long id) {
        userAppService.removeById(id);
        Map<String, Object> result = new HashMap<>();
        result.put("resultCode", "1");
        result.put("resultMessage", "保存成功!");
        return result;
    }

    /**
     * saveStaff:(添加员工). <br/>
     *
     * @param
     * @return
     * @throws Exception
     * @author Administrator
     * @since JDK 1.8
     */
    @PostMapping(value = "/saveStaff")
    @ResponseBody
    public Map<String, Object> saveStaff(StaffDTO staffDTO) {
        Result<Map<String, Object>> saveStaff = userAppService.saveStaff(staffDTO);
        Map<String, Object> result = saveStaff.getData();
        return result;
    }

    /**
     * deleteStaff:(删除员工). <br/>
     *
     * @param id
     * @return
     * @author Administrator
     * @since JDK 1.8
     */
    @PostMapping(value = "/deleteStaff")
    @ResponseBody
    public Map<String, Object> deleteStaff(Long id) {
        Result<Map<String, Object>> deleteStaff = userAppService.deleteStaff(id);
        Map<String, Object> result = deleteStaff.getData();
        return result;
    }

}
