package com.yijia.customer.controller;

import java.util.List;
import com.yijia.common.constant.HttpStatus;
import com.yijia.common.core.domain.entity.SysDept;
import com.yijia.common.core.domain.model.LoginUser;
import com.yijia.common.utils.SecurityUtils;
import com.yijia.common.utils.StringUtils;
import com.yijia.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yijia.common.annotation.Log;
import com.yijia.common.core.controller.BaseController;
import com.yijia.common.core.domain.AjaxResult;
import com.yijia.common.enums.BusinessType;
import com.yijia.customer.domain.CustomerElectronicCard;
import com.yijia.customer.service.ICustomerElectronicCardService;
import com.yijia.common.utils.poi.ExcelUtil;
import com.yijia.common.core.page.TableDataInfo;

/**
 * 客户电子会员卡（储蓄卡）Controller
 *
 * @author yijia
 * @date 2021-04-12
 */
@RestController
@RequestMapping("/customer/card")
public class CustomerElectronicCardController extends BaseController
{
    @Autowired
    private ICustomerElectronicCardService customerElectronicCardService;

    @Autowired
    private ISysDeptService deptService;
    /**
     * 查询客户电子会员卡（储蓄卡）列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CustomerElectronicCard customerElectronicCard)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        SysDept dept =new SysDept();
        dept.setDeptId(currentUser.getUser().getDeptId());
        List<String> list = deptService.selectDeptId(dept);
        if(list!=null && list.size()>0){
            customerElectronicCard.setStationIdList(list);
            customerElectronicCard.setStationId(null);
        }
        startPage();
        List<CustomerElectronicCard> cardList = customerElectronicCardService.selectCustomerElectronicCardList(customerElectronicCard);
        return getDataTable(cardList);
    }

    /**
     * 导出客户电子会员卡（储蓄卡）列表
     */
    @Log(title = "客户电子会员卡（储蓄卡）", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CustomerElectronicCard customerElectronicCard)
    {
        List<CustomerElectronicCard> list = customerElectronicCardService.selectCustomerElectronicCardList(customerElectronicCard);
        ExcelUtil<CustomerElectronicCard> util = new ExcelUtil<CustomerElectronicCard>(CustomerElectronicCard.class);
        return util.exportExcel(list, "card");
    }

    /**
     * 获取客户电子会员卡（储蓄卡）详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(customerElectronicCardService.selectCustomerElectronicCardById(id));
    }

    /**
     * 新增客户电子会员卡（储蓄卡）
     */
    @Log(title = "客户电子会员卡（储蓄卡）", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CustomerElectronicCard customerElectronicCard)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        customerElectronicCard.setStationId(currentUser.getUser().getDeptId());
        customerElectronicCard.setStationName(currentUser.getUser().getDept().getDeptName());
        return toAjax(customerElectronicCardService.insertCustomerElectronicCard(customerElectronicCard));
    }

    /**
     * 修改客户电子会员卡（储蓄卡）
     */
    @Log(title = "客户电子会员卡（储蓄卡）", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CustomerElectronicCard customerElectronicCard)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        customerElectronicCard.setStationId(currentUser.getUser().getDeptId());
        customerElectronicCard.setStationName(currentUser.getUser().getDept().getDeptName());
        return toAjax(customerElectronicCardService.updateCustomerElectronicCard(customerElectronicCard));
    }

    /**
     * 删除客户电子会员卡（储蓄卡）
     */
    @Log(title = "客户电子会员卡（储蓄卡）", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(customerElectronicCardService.deleteCustomerElectronicCardByIds(ids));
    }
    /**
     * 查询电子会员数据
     */
    @GetMapping("/listSum")
    public AjaxResult listSum(CustomerElectronicCard customerElectronicCard)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        SysDept dept =new SysDept();
        dept.setDeptId(currentUser.getUser().getDeptId());
        List<String> list = deptService.selectDeptId(dept);
        if(list!=null && list.size()>0){
            customerElectronicCard.setStationIdList(list);
            customerElectronicCard.setStationId(null);
        }
        return AjaxResult.success(customerElectronicCardService.listSum(customerElectronicCard));
    }
    @GetMapping("/selectCard")
    public TableDataInfo selectCard(CustomerElectronicCard customerElectronicCard)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        SysDept dept =new SysDept();
        dept.setDeptId(currentUser.getUser().getDeptId());
        List<String> list = deptService.selectDeptId(dept);
        if(list!=null && list.size()>0){
            customerElectronicCard.setStationIdList(list);
            customerElectronicCard.setStationId(null);
            customerElectronicCard.setStrSql(StringUtils.join(list, ","));
        }
       // startPage();
        List<CustomerElectronicCard>  querylist= customerElectronicCardService.selectCard(customerElectronicCard) ;
        List<CustomerElectronicCard> pagelist = customerElectronicCardService.selectCardPage(customerElectronicCard) ;
       //TableDataInfo  ss =getDataPageTable(querylist,pagelist);
//        PageInfo<CustomerElectronicCard> pageInfo= new PageInfo<CustomerElectronicCard>(querylist,customerElectronicCard.getPageSetting());
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
//        List<CustomerElectronicCard>  querylist= new ArrayList<>();
        rspData.setRows(querylist);
        rspData.setTotal(pagelist.size());
        return rspData;
    }
    /**
     * 查询电子会员数据
     */
    @GetMapping("/listHYData")
    public AjaxResult listHYData(CustomerElectronicCard customerElectronicCard)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        SysDept dept =new SysDept();
        dept.setDeptId(currentUser.getUser().getDeptId());
        List<String> list = deptService.selectDeptId(dept);
        if(list!=null && list.size()>0){
            customerElectronicCard.setStationIdList(list);
            customerElectronicCard.setStationId(null);
        }
        return AjaxResult.success(customerElectronicCardService.listHYData(customerElectronicCard));
    }

    /**
     * 查询电子会员数据
     */
    @GetMapping("/listJRHYData")
    public AjaxResult listJRHYData(CustomerElectronicCard customerElectronicCard)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        SysDept dept =new SysDept();
        dept.setDeptId(currentUser.getUser().getDeptId());
        List<String> list = deptService.selectDeptId(dept);
        if(list!=null && list.size()>0){
            customerElectronicCard.setStationIdList(list);
            customerElectronicCard.setStationId(null);
        }
        return AjaxResult.success(customerElectronicCardService.listJRHYData(customerElectronicCard));
    }
}
