package com.yijia.label.controller;

import java.util.Date;
import java.util.List;

import com.yijia.common.core.domain.model.LoginUser;
import com.yijia.common.utils.SecurityUtils;
import com.yijia.label.domain.RequstCustomerLabel;
import com.yijia.label.domain.ResponeCustomerLabel;
import com.yijia.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
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.label.domain.CustomerLabel;
import com.yijia.label.service.ICustomerLabelService;
import com.yijia.common.utils.poi.ExcelUtil;
import com.yijia.common.core.page.TableDataInfo;

/**
 * 用户标签Controller
 *
 * @author yijia
 * @date 2021-06-15
 */
@RestController
@RequestMapping("/label/label")
public class CustomerLabelController extends BaseController
{
    @Autowired
    private ICustomerLabelService customerLabelService;

    @Autowired
    private ISysDeptService deptService;
    /**
     * 查询用户标签列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CustomerLabel customerLabel)
    {
        startPage();
        List<CustomerLabel> list = customerLabelService.selectCustomerLabelList(customerLabel);
        return getDataTable(list);
    }

    /**
     * 导出用户标签列表
     */
    @Log(title = "用户标签", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CustomerLabel customerLabel)
    {
        List<CustomerLabel> list = customerLabelService.selectCustomerLabelList(customerLabel);
        ExcelUtil<CustomerLabel> util = new ExcelUtil<CustomerLabel>(CustomerLabel.class);
        return util.exportExcel(list, "label");
    }

    /**
     * 获取用户标签详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(customerLabelService.selectCustomerLabelById(id));
    }

    /**
     * 获取用户标签详细信息
     */
    @GetMapping(value = "/selectCustomerLabelInfo")
    public AjaxResult selectCustomerLabelInfo(CustomerLabel customerLabel)
    {
        System.out.println("==================="+new Date());
        LoginUser currentUser = SecurityUtils.getLoginUser();
        customerLabel.setStationId(currentUser.getUser().getDeptId());
        customerLabel.setDelFlag("0");
        ResponeCustomerLabel responeCustomerLabel =new ResponeCustomerLabel();
        List<CustomerLabel> notlist = customerLabelService.selectNotCustomerLabelInfo(customerLabel);
        List<CustomerLabel> list = customerLabelService.selectPhoneList(customerLabel);
        responeCustomerLabel.setNotCustomerLabelList(notlist);
       // responeCustomerLabel.setPhoneList(list);
        System.out.println("=================================="+new Date());
        return AjaxResult.success(responeCustomerLabel);
    }

    /**
     * 查询没有绑定标签的客户
     * @param customerLabel
     * @return
     */
    @GetMapping(value = "/selectNotCustomerLabelInfo")
    public TableDataInfo selectNotCustomerLabelInfo(CustomerLabel customerLabel)
    {
        startPage();
        LoginUser currentUser = SecurityUtils.getLoginUser();
        customerLabel.setStationId(currentUser.getUser().getDeptId());
        customerLabel.setDelFlag("0");
        List<CustomerLabel> notlist = customerLabelService.selectNotCustomerLabelInfo(customerLabel);
        return getDataTable(notlist);
    }

    /**
     * 查询以绑定标签的客户信息
     * @param customerLabel
     * @return
     */
    @GetMapping(value = "/selectPhoneList")
    public TableDataInfo selectPhoneList(CustomerLabel customerLabel)
    {
        startPage();
        LoginUser currentUser = SecurityUtils.getLoginUser();
        customerLabel.setDelFlag("0");
        customerLabel.setStationId(currentUser.getUser().getDeptId());
        List<CustomerLabel> list = customerLabelService.selectPhoneList(customerLabel);
        return getDataTable(list);
    }

    /**
     * 查询标签是否绑定了人
     * @param customerLabel
     * @return
     */
    @GetMapping(value = "/selectLabel")
    public AjaxResult selectLabel(CustomerLabel customerLabel)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        customerLabel.setStationId(currentUser.getUser().getDeptId());
        customerLabel.setDelFlag("0");
        List<CustomerLabel> list = customerLabelService.selectCustomerLabelList(customerLabel);
        boolean flag =true;
        if(list!=null && list.size()>0){
            flag=false;
        }
        return AjaxResult.success(flag);
    }

    /**
     * 新增用户标签
     */
    @Log(title = "用户标签", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CustomerLabel customerLabel)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        customerLabel.setCreateBy(currentUser.getUser().getUserId()+"");
        customerLabel.setDelFlag("0");
        return toAjax(customerLabelService.insertCustomerLabel(customerLabel));
    }

    /**
     * 修改用户标签
     */
    @Log(title = "用户标签", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CustomerLabel customerLabel)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        customerLabel.setUpdateBy(currentUser.getUser().getUserId()+"");
        return toAjax(customerLabelService.updateCustomerLabel(customerLabel));
    }

    /**
     * 修改用户标签
     */
    @Log(title = "用户标签", businessType = BusinessType.UPDATE)
    @PostMapping("/addAndDelCustomerLabel")
    @Transactional
    public AjaxResult addCustomerLabel(@RequestBody RequstCustomerLabel requstCustomerLabel)
    {
        int i =0;
        LoginUser currentUser = SecurityUtils.getLoginUser();
        if(requstCustomerLabel!=null && requstCustomerLabel.getFlag()!=null && requstCustomerLabel.getFlag().equals("0")){
            if(requstCustomerLabel.getPhoneList()!=null && requstCustomerLabel.getPhoneList().size()>0){
                for(String phone:requstCustomerLabel.getPhoneList()){
                    CustomerLabel customerLabel =new CustomerLabel();
                    customerLabel.setStationId(currentUser.getUser().getDeptId());
                    customerLabel.setDelFlag("0");
                    customerLabel.setPhone(phone);
                    customerLabel.setLabelId(requstCustomerLabel.getLabelId());
                    customerLabel.setCreateBy(currentUser.getUser().getUserId()+"");
                  i=  customerLabelService.insertCustomerLabel(customerLabel);
                }
            }
        }else if(requstCustomerLabel!=null && requstCustomerLabel.getFlag()!=null && requstCustomerLabel.getFlag().equals("1")){
            if(requstCustomerLabel.getPhoneList()!=null && requstCustomerLabel.getPhoneList().size()>0){
                for(String phone:requstCustomerLabel.getPhoneList()){
                    CustomerLabel customerLabel =new CustomerLabel();
                    customerLabel.setStationId(currentUser.getUser().getDeptId());
                    customerLabel.setDelFlag("1");
                    customerLabel.setPhone(phone);
                    customerLabel.setLabelId(requstCustomerLabel.getLabelId());
                    customerLabel.setUpdateBy(currentUser.getUser().getUserId()+"");
                    i=  customerLabelService.editCustomerLabel(customerLabel);
                }
            }
        }
        return toAjax(i);
    }

    /**
     * 删除用户标签
     */
    @Log(title = "用户标签", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(customerLabelService.deleteCustomerLabelByIds(ids));
    }
}
